【发布时间】:2019-01-17 06:41:45
【问题描述】:
我正在尝试比较条件 x 与 y 中的值。这是我的数据:
mydata <- structure(list(
Row.names = c("ASPTAm", "ATPtmB_MitoCore", "CI_MitoCore",
"CIII_MitoCore", "CIV_MitoCore", "CO2t", "CO2tm", "CV_MitoCore",
"H2Ot", "H2Otm", "MDH", "MDHm", "O2t", "O2tm", "OF_ATP_MitoCore",
"PGK", "PGM", "PIt2mB_MitoCore", "SUCOASm"),
mean_x = c(-1.416333333,
26.1376024, 8.444222444, 9.983111555, 4.991555778, -5.06, -5.055,
24.43926907, -4.719, -30.9051024, -1.580333333, 3.093666667,
5, 5, 29.7476024, -1.81, -1.81, 25.9436024, -1.698333333),
mean_y = c(-2.455e-14,
78.68825722, 51.30062794, 9.897398744, 4.948699372, -40.05114286,
-40.15114286, 68.14247151, -29.51685714, -108.3586144, -60.164,
10.90278571, 5, 5, 82.39825722, -1.81, -1.81, 86.41154294, -10.58878571
)),
class = "data.frame", row.names = c(NA, -19L),
.Names = c("Row.names", "mean_x", "mean_y"))
我想要一个点图,每组之间有一条线连接。这是我尝试过的:
library(reshape2)
library(ggplot2)
mydata <- melt(mydata)
ggplot(mydata, aes(x = Row.names, y = value, color = variable)) +
geom_point(stat = 'identity', position = position_dodge(width = 1.0), size = 2.5) +
theme(axis.text.x = element_text(angle = 60, hjust = 1))
这是我得到的情节:
我还想连接每个变量中的两个点。
如果我按 Row.names 对数据进行分组,然后添加 geom_line(),它会起作用,但每组中的点再次重叠。
如何在连接点时将它们分开?
【问题讨论】:
-
如果你想通过
variable连接点,在aes中使用group = variable,如果你想用一条线使用group = 1。另外,您必须添加+ geom_line(如果您只想要一行,则必须添加geom_line(color = "black"))。 -
@PoGibas 我认为这不是 OP 的问题。 @Prashant,你想连接唯一的
Row.names的点,但保留position_dodge()? -
@PoGibas 我希望线以某种方式连接显示here,同时稍微移动 variable.y 的点,以免它们重叠。
-
@Prashant @LAP 链接的问题中的第二个答案应该解决您的用例吗?类似
ggplot(mydata, aes(x = as.numeric(interaction(variable, Row.names)), y = value, color = variable, group = Row.names)) + geom_point() + geom_line() + scale_x_continuous(breaks = seq(1.5, nrow(mydata) - 0.5, 2), labels = sort(unique(mydata$Row.names))) + theme(axis.text.x = element_text(angle = 60, hjust = 1), panel.grid.minor.x = element_blank())