我有一个这样的数据集
df<- structure(list(X = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L,
3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L), .Label = c("Control1",
"Control2", "Control3", "Control4", "Control5", "Control6", "Control7"
), class = "factor"), name = structure(c(3L, 2L,
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L,
3L, 2L, 1L), .Label = c("Cond1", "Cond2", "Cond3"), class = "factor"),
Value = c(1.1, 1.7, -0.7, 0.8, 1.9, -0.71, 1.6, 1.6, -0.5,
1.9, 1.8, -0.6, 1.7, 1.5, -0.9, 1.3, 1.5, -0.7, 1.2, 1.6,
-0.5), Signifcance = c(1.06e-18, 9.4e-20, 9e-04, 5.7e-10,
3.02e-50, 8.01e-08, 1.4e-19, 8.6e-83, 3.2e-07, 6.8e-15, 1.2e-92,
1.7e-09, 6.2e-15, 3.4e-68, 1.4e-09, 6.9e-15, 2.3e-48, 4.3e-12,
1.1e-21, 1.7e-141, 1.4e-08)), class = "data.frame", row.names = c(NA,
-21L))
我一直在努力找出如何根据第一列在彼此之上绘制数据
如果查看X列,则有Control1(3次)Control2(3次)等。在每个控件中,我有3个条件 我想让3条线与所有Control上的每个cond相对应,而不是一条轴,然后一条线代表每种颜色的有效值,并带有另一种颜色。
我在此处检查并找到了此解决方案,但我不知道如何将其用于此数据 How can I plot with 2 different y-axes?
感谢您的帮助
答案 0 :(得分:1)
我不确定您想要的结果图看起来如何,但这是否按照您想要的方向进行?
mat = matrix(c(1,1,1,2,3,4), 3, 2, byrow = F)
layout(mat)
plot(df[df$name == "Cond1",]$Value, col="red", type="l", ylim=c(min(df$Value), max(df$Value)), ylab="Value", main="Values")
lines(df[df$name == "Cond2",]$Value, col="blue", type="l")
lines(df[df$name == "Cond3",]$Value, col="green", type="l")
legend("right", legend = c("Cond1","Cond2","Cond3"), lwd = c(1,1,1), col=c("red","blue","green"))
plot(df[df$name == "Cond1",]$Signifcance, col="red", lty=2, type="l", ylab="Sign.", main="Significance")
plot(df[df$name == "Cond2",]$Signifcance, col="blue", lty=2, type="l",ylab="Sign.")
plot(df[df$name == "Cond3",]$Signifcance, col="green", lty=2, type="l",ylab="Sign.")
legend("right", legend = c("Cond1","Cond2","Cond3"), lty = 2, col=c("red","blue","green"))
或者使用ggplot2的 dev版本,您可以轻松地将有效值分成3个具有不同y轴的图。
devtools::install_github("tidyverse/ggplot2")
library(ggplot2)
library(gridExtra)
plot1 <- ggplot(df, aes(X, Value, group=name, color=name)) +
geom_line() +
geom_point() +
ggtitle("Values")
plot2 <- ggplot(df, aes(X, Signifcance, group=name, color=name)) +
geom_line() +
facet_grid(rows=vars(name), scales = "free_y")+
ggtitle("Signifcance")
grid.arrange(plot1, plot2, ncol=2)