我有一个带有一些列的表,比方说:1910-1935年,出售产品的数量,以及最后一个列中的一些算术平均值。
我尝试将其绘制在R中的ggplot中。并且我得到了适当的年均值图。但是我的问题是: 我需要在已销售多年的产品上添加一条线。它们具有完全不同的值(第一个图的Y值:0-3.5,第二个图的0-1400)。因此,如果我尝试添加第二行,则第一个值将变平。
我如何包含第二张图表,以便年份基于X轴(即,从第一张图表开始的年份),并且已售出的产品在右侧获得新的Y2轴(以便它们不会使图表变平)?
我试图将新的绘图添加到另一个绘图中,但是Y2不适合第一个绘图区域(我在绘图区域内有一个Y2轴)。
代码:
par(mar=c(5,5,5,5))
ggplot(prod, aes(year, mean)) +
geom_line(colour = "red")
#ggplot(prod, aes(year, numOfProd)) + geom_line()
ggplot(prod, aes(year, mean)) + geom_smooth(method="glm", formula=y~poly(x,3), se=FALSE, colour ="red") +
geom_smooth(method = "lm", formula=y~x, se = FALSE, colour = "blue") +
stat_smooth(method = "lm", colour = "blue") +
#geom_point() +
xlab("year of sold") + ylab("Mean[%]") +
scale_x_continuous(breaks = seq(min(inbred_wszystko$rur), max(prod$year), by = 5)) +
scale_y_continuous(breaks = seq(min(prod$mean), max(prod$mean), by = 0.2))
par(new=TRUE)
## Plot the second plot and put axis scale on right
par(mar=c(5,5,5,5))
plot(prod$year, all_prod$numOfprod, xlab="", ylab="", ylim=c(0,1500),
axes=FALSE, type="l", col="dark green")
mtext("Example",side=4,col="black",line=4)
axis(4, ylim=c(0,1500), col="black",col.axis="black",las=2)
所以恢复: 我需要从第二个图(在第二个注释之后)到第一图添加一条线。它们基于相同的X轴,但Y上的值不同。而且我还需要在绘图的右侧为该线添加Y2轴。有人可以帮忙吗?
编辑: 样本数据:
year mean soldProd
1910 0.5 798
1911 0.6 4234
1912 0.3 25
1913 0.1 2423
1914 0.6 4242
1915 0.3 5
1916 0.1 21
1917 0.11 442
1918 0.5 2353
1919 0.6 23
1920 0.3 42
1921 0.1 34
1922 0.3 235
1923 0.1 2
1924 0.5 5
1925 0.5 23
1926 0.5 235
1927 0.6 23
1928 0.3 4
1929 0.1 234
1930 0.5 2
1931 0.5 5
1932 0.5 2
1933 0.6 6
1934 0.3 4
1935 0.1 36