I'm plotting each subject's proportion of responses to "same" or "different" stimuli across two blocks (A block and B block), meaning that each subject has 4 data points plotted. Two for A block (same and different) and two for B block (same and different). Also means there are 4 rows of data containing proportion of responses for each subject.
I would like to connect each subject's "same" and "different" data points with a line WITHIN a block (e.g. their one datapoint for "same" and datapoint for "different" are connected via a line within the A block and within the B block for each subject).
I tried using the "group = subject" that comes with geom_line(), but it connects ALL of the subject's data points when I would like to only connect data points within each block.
testplot <- ggplot(df, aes(x=block, y=prop, shape=con, colour=con)) +
geom_point(position=position_dodge(width=.1)) +
xlab("Block") + ylab("Prop of responses") +
theme_bw(base_size = 13) +
theme(legend.position="top")
plot(testplot)
sample dataset:
subj = c(1, 1, 2, 2, 3, 3, 4, 4, 1, 1, 2, 2, 3, 3, 4, 4)
prop = c(0.5, 0.8, 0.3, 0.7, 0.9, 0.4, 0.1, 0.5, 1, 0.5, 0.9, 0.2, 0.7, 0.4, 0.8, 0.1)
con = c("same", "same", "same", "same", "same", "same", "same", "same", "diff", "diff", "diff", "diff", "diff", "diff", "diff", "diff")
block = c("A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B")
df = data.frame(subj, prop, con, block)
I hope this makes sense, is this feasible? Any help is appreciated!
pairs (data) The pairs R function returns a plot matrix, consisting of scatterplots for each variable-combination of a data frame. The basic R syntax for the pairs command is shown above. In the following tutorial, I’ll explain in five examples how to use the pairs function in R.
The modified pairs plot has a different color, diamonds instead of points, user-defined labels, and our own main title. For even more options, have a look at the help documentation of pairs by typing ?pairs to the RStudio console. Quite often you will have different subsets or subgroups in your data.
We can also draw a regression line to our scatterplot by using the abline and lm R functions: Figure 2: Draw Regression Line in R Plot. Figure 2 shows the same scatterplot as Figure 1, but this time a regression line was added.
You can create this type of chart in base R with the plot function, setting type = "b". # Data x <- c(1, 2, 3, 4, 5, 4, 7, 8, 9) y <- c(12, 16, 14, 18, 16, 13, 15, 20, 22) # Connected scatter plot plot(x, y, type = "b", xlab = "Var 1", ylab = "Var 2") The symbol used by default when type = "b" can be modified making use of the pch argument.
I think the best approach is with a facet_grid
:
ggplot(df, aes(x=con, y=prop, shape=con)) +
geom_point(aes(color=con), position=position_dodge(width=.1)) +
geom_line(aes(group = subj)) +
facet_grid(.~block) +
xlab("Block") + ylab("Prop of responses") +
theme_bw(base_size = 13) +
theme(legend.position="top")
From looking at the other answers here, I perhaps misinterpreted, but I think this is possibly a clearer representation anyway:
ggplot(df, aes(factor(subj), prop)) +
geom_point(aes(fill = con), size = 3, shape = 21) +
geom_line(aes(group = factor(subj))) +
facet_grid(.~block, switch = "x") +
scale_x_discrete(expand = c(2, 2)) +
labs(x = "Subjects within block") +
theme_classic() +
theme(text = element_text(size = 12),
strip.placement = "outside",
strip.background = element_blank(),
panel.grid.major.y = element_line(colour = "gray90"))
One approach to achieve your desired plot would be to use facet_wrap
by block
, map con
on x
and subj
on group
. Try this:
subj = c(1, 1, 2, 2, 3, 3, 4, 4, 1, 1, 2, 2, 3, 3, 4, 4)
prop = c(0.5, 0.8, 0.3, 0.7, 0.9, 0.4, 0.1, 0.5, 1, 0.5, 0.9, 0.2, 0.7, 0.4, 0.8, 0.1)
con = c("same", "same", "same", "same", "same", "same", "same", "same", "diff", "diff", "diff", "diff", "diff", "diff", "diff", "diff")
block = c("A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B")
df = data.frame(subj, prop, con, block)
library(ggplot2)
testplot <- ggplot(df, aes(x=con, y=prop, group = subj)) +
geom_point(aes(shape=con, colour=con)) +
geom_line() +
xlab("Block") + ylab("Prop of responses") +
theme_bw(base_size = 13) +
theme(legend.position="top") +
facet_wrap(~block, strip.position = "bottom") +
theme(strip.placement = "outside", axis.text.x = element_blank(), axis.ticks.x = element_blank())
testplot
This answer is very similar to the one by Ian Cambell but with colour and shape used to represent the subject since the same/diff is shown on the x-axis and is therefore redundant in the legend. With the subjects easily identifiable, the plot is also similar to the one by Allan Cameron. Just to show another perspective.
ggplot(df, aes(x=con, y=prop, col=subj, shape=subj)) +
geom_point() +
geom_line(aes(group = subj)) +
facet_grid(.~block) +
xlab("Block") + ylab("Prop of responses") +
theme_bw(base_size = 13) +
theme(legend.position="top")
We can then compare the four subjects across the blocks (facets).
These are all good suggestions and interesting ideas if you want to reconsider your data representation (facets in particular), but to actually answer your question, you want to paste the groups together, as in geom_line(aes(group=paste(subj, con)))
ggplot(df, aes(x=block, y=prop, shape=con, colour=con)) +
geom_point() +
geom_line(aes(group=paste(subj, con))) +
xlab("Block") + ylab("Prop of responses") +
theme_bw(base_size = 13) +
theme(legend.position="top")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With