本文介绍了将独立于 y 的线添加到 R 中的轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些列的表格,比方说:1910-1935 年、已售产品数量以及最后一列的一些算术平均值.

I have a table with some column, let's say: years 1910-1935, number of sold product, and some arithmetic mean in the last col.

我尝试在 R 中的 ggplot 中绘制它.我得到了一个正确的 year~mean 绘图.但我的问题是:我需要在该图中添加一条线,其中包含多年销售的产品.它们具有完全不同的值(第一个图的 Y 值:0 - 3.5,第二个:0 - 1400).因此,如果我尝试添加第二行,则第一个值会变平.

I try to plot it in ggplot in R. And I got a proper plot for year~mean. But my problem is:I need to add a line to that plot with years~sold products. They have a completely different value (the first plot have a Y value: 0 - 3.5, second: 0 - 1400). So if I try to add my second line, the first values are flatting.

如何包含第二个图表,以便年份基于 X 轴(即第一个图表中的年份),并且已售产品在右侧获得新的 Y2 轴(以便它们不会使图表变平)?

How can I include the second chart so that the years are based on the X axis (i.e. years from the first chart) and sold products get a new Y2 axis on the right (so that they do not flatten the chart)?

我尝试将新绘图添加到另一个绘图,但 Y2 不适合第一个绘图区域(我在绘图区域内有一个 Y2 轴).

I tried to add new plot to another, but the Y2 didn't fit to first plot area (i had a Y2 axis inside a plot area).

代码:

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 轴.有人可以帮忙吗?

So resume:I need to add a line from the second plot (after the second comment) to first plot. They based on the same X.axis but values on Y are different. And also I need to add Y2 axis for that line on right side of the plot. Can someone help?

示例数据:

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

推荐答案

您可以使用 ggplotsec.axis 选项.

You can use ggplot's sec.axis option.

这是一个使用一些样本数据的示例,它应该与您的数据有些相似.

Here is an example using some sample data that should be somewhat comparable to yours.

library(ggplot2)
library(scales)


prod <- data.frame(year=1910:1935, mean=sample(seq(0.1, 0.6, 0.1), 26, replace = T), soldProd=sample(4:2000, 26))

gg <- 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[%]")

gb <- ggplot_build(gg)
y.range <- gb$layout$panel_params[[1]]$y.range
y2.range <- extendrange(range(prod$soldProd), f=0.01)
prod$scaledSoldProd <- rescale(prod$soldProd, y.range, y2.range)
scale_factor <- (diff(y.range)/max(y2.range))
trans <- ~ ((. -y.range[1])/scale_factor)

gg <- gg + geom_line(aes(y=scaledSoldProd))
gg <- gg + scale_y_continuous(breaks = seq(min(prod$mean), max(prod$mean), by = 0.2),
                              sec.axis = sec_axis(trans, name = "Number of products"))

print(gg)

这篇关于将独立于 y 的线添加到 R 中的轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:40