Reputation: 141
I have a data set similar to the example data in the code below. I would like to connect the two points from the same ID in the two conditions, so as to better see the individual change from one condition to the other. Each individual is only part of one "group" but has a value for each of the two conditions. Thanks for any ideas!
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")
Upvotes: 0
Views: 1371
Reputation: 141
This is how I changed by exemplar data and made the plot as suggested in the comment from tkerwin above, thank you again!
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)
Upvotes: 0
Reputation: 9759
Perhaps you want something like this:
pd = position_dodge(width=0.4)
ggplot(exampleData, aes(x=factor(condition), y=PropYes,
color=factor(group), group=factor(ID))) +
geom_point(position=pd) + geom_line(position=pd) +
theme_pander()+scale_color_ptol("condition")
Upvotes: 2