Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中有2个y轴的图上平滑我的线?_R_Ggplot2_Graph - Fatal编程技术网

如何在R中有2个y轴的图上平滑我的线?

如何在R中有2个y轴的图上平滑我的线?,r,ggplot2,graph,R,Ggplot2,Graph,我有一个y轴为2的图,我在上面画了一个条形图和一个线形图。线图每个月有1个值,但我希望平滑线图,同时仍保持2 y轴。当我使用函数时对y轴为2的图形不起作用,样条曲线给我的值与原始值完全不同 以下是一些与我所拥有的类似的示例数据: month_new = month.abb Temp = sample(25, 12) mapoc_temp = cbind(month_new, Temp) num_unique_tags = sample(25, 12) month = month.abb bar

我有一个y轴为2的图,我在上面画了一个条形图和一个线形图。线图每个月有1个值,但我希望平滑线图,同时仍保持2 y轴。当我使用函数
对y轴为2的图形不起作用,
样条曲线
给我的值与原始值完全不同

以下是一些与我所拥有的类似的示例数据:

month_new = month.abb
Temp = sample(25, 12)
mapoc_temp = cbind(month_new, Temp)

num_unique_tags = sample(25, 12)
month = month.abb
bargraph_dets_temp = cbind(month, num_unique_tags)

我使用了下面的代码来绘制这些数据

ggplot(bargraph_dets_temp, 
       aes(x = month, y = num_unique_tags)) +
  #for my detection data
  geom_col(position = position_dodge()) +
  #For my temperature data
  geom_line(inherit.aes = FALSE, data = mapoc_temp,
            aes(x = month_new, y = as.numeric(Temp) + 2, group = 2),
            color = "forestgreen", size = 2) +
  #Make sure the 2 graphs share the same x axis
  scale_x_discrete(limits = c("Jan","Feb","Mar","Apr","May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) +
  #putting 2 y scales on one graph
  scale_y_continuous(limits = c(0,30), name = "Total Unique Detections", 
                     sec.axis = sec_axis(~ . -2 , name = "Temperature (°C)"))


它给我的图片是这样的


我不关心线图的细节级别,我只想展示一个递增和递减值的一般模式,有人知道如何使线看起来更平滑吗?

最简单的方法是用样条线插值数据。您应该意识到的第一件事是
month
month\u new
ggplot
视为数值
1:12
。但是,使用自定义限制绘制离散x轴的方式会带来麻烦。相反,为了更好地控制此映射,您应该将离散x值编码为一个因子,在其级别上具有给定的顺序:

plot_data <- data.frame(month = factor(month.abb, levels = month.abb), 
                        temp = sample(25, 12),
                        num_unique_tags = sample(25, 12))
要进行插值,只需使用插值数据创建一个新的数据集,并绘制:

plot_data_interp <- as.data.frame(spline(x = 1:12, y = plot_data$temp, xout = seq(1, 12, length = 100)),
                               col.names = c('month', 'temp'))

ggplot(plot_data, aes(x = month, y = num_unique_tags)) + 
  geom_col() + 
  geom_line(aes(y = temp, group = 2), data = plot_data_interp, color = "forestgreen", size = 2) +
  scale_y_continuous(limits = c(0, 30), name = "Total Unique Detections", 
                     sec.axis = sec_axis(~ . -2 , name = "Temperature (°C)"))

plot\u data\u interp从
ggalt
包中尝试
geom\u-pline
而不是
geom\u-line
这是否仍然适用于我的其余ggplot内容?你尝试过
geom\u-smooth
而不是
geom\u-line
吗?(). 默认情况下,它使用的是
。好的,我现在意识到可能需要更多的数据点来平滑它。我每个月只有1个值,平滑在这里有效吗?我不是很好,但你可以试试。另外,我不确定
geom_smooth
是否能与离散的vlaues一起工作,因此您可能需要将数据协调为日期格式为什么
length=100
?。。。另外,如果我的mapoc_temp数据框第三列有年份,总共是3年,那么36个月将
x=1:36
,然后
seq(1,36…
?长度100只是为了制作更多的点,从而显示平滑的插值曲线。我有两个不同的数据帧中的数据。我提供的数据只是样本。我的数据帧有不同数量的变量。有没有一种方法不必将
temp
num\u unique\u dets的数据帧组合起来de>@KristenCyr插值和平滑示例实际上创建了一个新的数据集(
plot\u data\u interp
plot\u data\u smooth
),所以您应该在
mapoc_temp
中插入/平滑数据,然后将其放入
plot_data_interp
/
plot_data_平滑
。重要的是将
month
变量构造为一个因子,其中
levels
参数给出正确的值顺序(如果您有多年,例如
levels=c('2017年12月'、'2018年1月'、'2019年12月')
。您的插值/平滑应使用
x=1:lenght(条形图\u dets\u temp$month)
作为样条函数的x值。
plot_data_interp <- as.data.frame(spline(x = 1:12, y = plot_data$temp, xout = seq(1, 12, length = 100)),
                               col.names = c('month', 'temp'))

ggplot(plot_data, aes(x = month, y = num_unique_tags)) + 
  geom_col() + 
  geom_line(aes(y = temp, group = 2), data = plot_data_interp, color = "forestgreen", size = 2) +
  scale_y_continuous(limits = c(0, 30), name = "Total Unique Detections", 
                     sec.axis = sec_axis(~ . -2 , name = "Temperature (°C)"))
plot_data_smooth <- as.data.frame(predict(smooth.spline(x = 1:12, y = plot_data$temp, spar = 0.5), x = seq(1, 12, length = 100)),
                                  col.names = c('month', 'temp'))

ggplot(plot_data, aes(x = month, y = num_unique_tags)) + 
  geom_col() + 
  geom_line(aes(y = temp, group = 2), data = plot_data_smooth, color = "forestgreen", size = 2) +
  scale_y_continuous(limits = c(0, 30), name = "Total Unique Detections", 
                     sec.axis = sec_axis(~ . -2 , name = "Temperature (°C)"))