我正在尝试绘制以下数据的图表:
ph1 = c(5, 6, 7, 8, 9)
ph2 = ph3 = ph1
e1 = c(0.191, 0.154, 0.179, 0.073, 0.009)
e2 = c(0, 0.029, 0.054, 0.055, 0.024)
e3 = c(0.019, 0.027, 0.063, 0.029, 0.039)
set.seed(1)
df1 <- data.frame(e1 = sort(runif(5, 0.05, 0.25)),
e2 = sort(runif(5, 0.05, 0.25)),
e3 = sort(runif(5, 0.05, 0.25)),
ph1 = sort(runif(5, 1, 100)),
ph2 = sort(runif(5, 1, 100)),
ph3 = sort(runif(5, 1, 100))
)
### reshape this to give a column indicating group
df2 <- with(df1,
as.data.frame(cbind( c(ph1, ph2, ph3),
c(e1, e2, e3),
rep(seq(3), each=5) )
))
colnames(df2) <- c("ph","maltose_in_mg","team")
df2$team <- as.factor(df2$team)
library(ggplot2)
ggplot(df2, aes(x=ph, y=maltose_in_mg, col=team)) + geom_line()
...x 轴上的 pH 值(5 到 9)作为标签。不幸的是,标签显示的范围是 0 到 100。
编辑(注意:非功能性解决方案):
df1 <- data.frame(e1 = sort(runif(5, 0.05, 0.25)),
e2 = sort(runif(5, 0.05, 0.25)),
e3 = sort(runif(5, 0.05, 0.25)),
ph1 = sort(runif(1, 5, 9)),
ph2 = sort(runif(1, 5, 9)),
ph3 = sort(runif(1, 5, 9))
)
最佳答案
我认为这就是您想要做的,但如果不是,请纠正我。
我将 pH 值放在 x 轴上,将麦芽糖(以毫克为单位)放在 y 轴上。
我还从 ggthemes
包中添加了一个主题,这使得它很漂亮。您可以在我在代码注释中放置的链接中找到其他主题。
最后,我用 coord_cartesian
限制了 x 轴(<-- 这就是我认为您正在寻找的,控制显示的范围),然后 scale_colour_discrete
在图例上放置一个漂亮的标题。
接下来我添加了一个带有 ggtitle
的标题和轴标签。 theme_wsj()
具有 ggplot 设置,您只需在 R
控制台中输入 theme_wsj()
即可查看这些设置。它的默认设置是隐藏轴标签,我必须使用 theme()
函数及其内部的位来覆盖它。
ggplot2
是一个令人惊叹的软件包。您可以在这里阅读所有相关内容:http://docs.ggplot2.org/current/
install.packages("ggthemes")
library(ggthemes)
ph1 = c(5, 6, 7, 8, 9)
ph2 = ph3 = ph1
e1 = c(0.191, 0.154, 0.179, 0.073, 0.009)
e2 = c(0, 0.029, 0.054, 0.055, 0.024)
e3 = c(0.019, 0.027, 0.063, 0.029, 0.039)
set.seed(1)
df1 <- data.frame(e1 = sort(runif(5, 0.05, 0.25)),
e2 = sort(runif(5, 0.05, 0.25)),
e3 = sort(runif(5, 0.05, 0.25)),
ph1 = sort(runif(1, 5, 9)),
ph2 = sort(runif(1, 5, 9)),
ph3 = sort(runif(1, 5, 9))
)
df2 <- with(df1,
as.data.frame(cbind( c(ph1, ph2, ph3),
c(e1, e2, e3),
rep(seq(3), each=5) )
))
colnames(df2) <- c("ph", "maltose_in_mg", "team")
df2$team <- as.factor(df2$team)
library(ggplot2)
ggplot(df2, aes(x = ph, y = maltose_in_mg, colour = team)) +
geom_line(size = 2) +
coord_cartesian(xlim = c(5, 9)) + # this is how you limit the axis range displayed, you can do the y, too with ylim = c(0, 1)
scale_colour_discrete(name = "Team") +
theme_wsj() + # find more themes here: https://github.com/jrnold/ggthemes
ggtitle("pH by Team") +
ylab("Maltose in Milligrams") +
xlab("pH") +
theme(axis.title = element_text(angle = 90, vjust = -0.075),
axis.text = element_text(size = 20),
legend.text = element_text(size = 15))
关于r - 在图表的 x 轴上设置标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19037565/