Hello stackoverflow社区,我有一个关于ggplot编码的问题。这是我的代码,数据格式和输出目前和下面是我的问题。
数据格式:
ID time var1 var2 var3
a 1 2 3 4
a 5 6 7 8
b 9 11 12 13
b 14 15 16 17
c . . . .
c . . . .
and so forth
代码:
gg1 <- ggplot() + geom_line(aes(x=TIME, y=Var1, col="red"), FILE) +
geom_line(aes(x=TIME, y=Var2, col="blue"), FILE) +
geom_point(aes(x=TIME, y=Var3), Model_20160806) + facet_wrap( ~ ID)+
xlab("Time (Hr)") + ylab("Concentration (ng/ml)") + ggtitle("x")
我一直在努力制作正确格式的情节,非常感谢任何帮助。
如您所见,col =&#34;红色/蓝色&#34;显示为图例而不是颜色?有办法解决吗?
如何在输出底部添加Var1,Var2,Var3的图例?
我尝试将facet_wrap(~ID,ncol = 3)添加到代码中,但它不起作用并提供null。有办法解决这个问题吗?
由于有很多细胞样本,有没有办法让图形在多个页面上,因此图形可见且可解释
最后,为了更好地显示转染数据,我尝试使用gg1+theme_bw()
,但这不起作用。
答案 0 :(得分:1)
如果没有可重复的示例,很难帮助您解决这些问题。
aes(..., col="blue")
不起作用。在aes()
内,所有内容都必须引用您的数据框的一列。如果数据框中有分组变量,请使用它来定义颜色。如果您希望所有内容都只是蓝色,请在aes()
之外定义颜色。
像scale_colour_manual(values=c("red","green","blue"))
这样的东西。来自Add legend to ggplot2 line plot的可能重复的问题。
您能解释一下您想对facet_wrap( ~ ID, ncol=3)
做什么吗?
是的,这是可能的。最简单的方法是通过将x
分成10个组来制作多个图表。
这也是您需要可重复示例的原因。简短的回答是,theme_bw()
对我有用,我不知道为什么它不适合你。
例如:
library(car)
library(ggplot2)
data("diamonds")
ggplot(diamonds, aes(x = carat, y = cut, color = color)) +
geom_point() +
theme_bw()
编辑:举例说明将数据框拆分为10个组:
# Example data
df = data.frame(x = factor(rep(1:30, each = 10)), y1 = rnorm(300), y2 = rnorm(300))
# Assume that df$x is the grouping variable consisting of too many groups
# Every df$x < 10 becomes 0, 10 < df$ < 20 becomes 1, etc.
df$x2 = floor(as.numeric(df$x) / 10)
# Split the dataframe based on this new grouping variable df$x2
dfSplit = split(df, df$x2)
# do a loop over dfSplit
for (i in 1:length(dfSplit)) {
dfForPlotting = dfSplit[[i]]
# do plotting stuff
ggplot(data = dfForPlotting, aes(x = y1, y = y2, color = x)) + geom_line()
}
答案 1 :(得分:0)
关于问题2,最简单的方法是使用网格包和grid.text()
。
library(grid)
par(mar=c(6.5, 2, 2, 2))
plot(1:10,1:10)
grid.text(x=0.2, y = 0.05, "Var1 = Birds, Var2 = Bees")