[英]ggplot in R: how to draw a line through corresponding dodged points
我有一个类似于下面代码中的示例数据的数据集。 我想在两个条件下连接来自同一ID的两个点,以便更好地查看从一个条件到另一个条件的个体变化。 每个人只是一个“组”的一部分,但是对于两个条件中的每一个都有一个值。 感谢您的任何想法!
library(ggplot2)
library(ggthemes)
ID <- c(1,1,2,2,3,3,4,4,5,5)
group <- c(20,20, 50, 50,20, 20, 80, 80, 80, 80)
condition <- c("med", "placebo","med", "placebo","med", "placebo","med", "placebo","med", "placebo")
PropYes <- c(0.13, 0.15, 0.25, 0.13, 0.54, 0.34, 0.23, 0.45, 0.142, 0.344)
exampleData <- data.frame(ID, group, condition, PropYes)
exampleData <- within(exampleData, {
group <- as.factor(group)
condition <- as.factor(condition)
})
#plot
p <- ggplot(exampleData, aes(x = group, y = PropYes, fill = condition))
p + geom_point(aes(colour = factor(condition)),position=position_dodge(width = 0.4)) +
theme_pander()+scale_color_ptol("condition")
这就是我根据示例数据更改的方法,并按照上面tkerwin的评论中的建议进行绘制,再次感谢!
library(ggplot2)
library(ggthemes)
library(tidyr)
library(dplyr)
ID <- c(1,1,2,2,3,3,4,4,5,5)
group <- c(20,20, 50, 50,20, 20, 80, 80, 80, 80)
condition <- c("med", "placebo","med", "placebo","med", "placebo","med", "placebo","med", "placebo")
PropYes <- c(0.13, 0.15, 0.25, 0.13, 0.54, 0.34, 0.23, 0.45, 0.142, 0.344)
exampleData <- data.frame(ID, group, condition, PropYes)
exampleData <- within(exampleData, { group <- as.factor(group) condition <- as.factor(condition) })
plotdata <- exampleData %>%
spread(condition, PropYes)%>%
rename(medication = med,
placebo = placebo)
p <- ggplot(plotdata, aes(x = medication, y = placebo, color= group))
p + geom_point(size = 3) +
theme_tufte() +
geom_abline(intercept = 0, slope = 1, alpha = 0.4, linetype=2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.