Thank you! I'm using two data sets because one has the means and sd, I was missing the group = SAMPLE, thing. Thank you very much
Hello, I'm struggling doing a plot with R and I didn't find an answer, so I post it here and if you know the answer or a post with the answer it would be really helpful.
I want a barplot with error bars, and with a dot plot with the values of every single replicate stacked by target and sample, but I want the dots with a different shape for each biological replicate
What I want is a graph like the second one but with the dots shaped like in the first one, I tried different options but I still dont get the result I want. Here it is my code:
ggplot(summ, aes(x = TARGET, y = value, fill = factor(SAMPLE, levels=c("Ctrl", "si")))) +
geom_bar(position = position_dodge(), stat = "identity", colour = "black", width = .9) +
geom_errorbar(aes(ymin=value-sd, ymax=value+sd), width=.2,
position=position_dodge(.9)) + theme_minimal() +
scale_fill_manual(values = c("#CCCCCC", "#FFFFFF")) +
ggtitle("G30") +
theme(axis.title.x = element_blank()) + ylab("Fold Change\n") +
#scale_x_discrete("TARGET", limits = c()) +
guides(fill = guide_legend(title = NULL)) +
geom_jitter(data = dt, aes( x = TARGET, y =value,
fill =factor(SAMPLE, levels=c("Ctrl", "si"))),
#shape = variable),
position = position_dodge(width = 0.9), stat = "identity")
And here an example of my input:
TARGET SAMPLE variable value
a Ctrl REPL.1 0.8435998
a si REPL.1 0.7391897
b Ctrl REPL.1 0.7636603
b si REPL.1 0.7921603
c Ctrl REPL.1 0.4640940
c si REPL.1 0.3322137
Thank you!
2 answers
Thanks for the example, now I finally understood the issue. It's basically what's been described here.
Some comments:
- Is it intentional that you're using two different objects for the bar chart (
summ) and the points (dt)? - you could get a similar effect by binding x to SAMPLE and use TARGET as a faceting factor
- you will need to add
group = SAMPLEtogeom_point()if you want to keep the alignment of the points with the alignment of the bars
This both worked for me:
summ <- data.frame( TARGET = rep(c("a","a","b","b", "c","c"),2),
SAMPLE = rep(c("Ctrl","si","Ctrl","si","Ctrl","si"),2),
variable = c(rep("REPL.1", 6), rep("REPL.2",6)),
value = c(0.8435998, 0.7391897, 0.7636603, 0.7921603, 0.4640940,0.3322137,
0.8+0.2, 0.7+0.2, 0.7+0.2, 0.7+0.2, 0.4+0.2,0.3+0.2))
> summ
TARGET SAMPLE variable value
1 a Ctrl REPL.1 0.8435998
2 a si REPL.1 0.7391897
3 b Ctrl REPL.1 0.7636603
4 b si REPL.1 0.7921603
5 c Ctrl REPL.1 0.4640940
6 c si REPL.1 0.3322137
7 a Ctrl REPL.2 1.0000000
8 a si REPL.2 0.9000000
9 b Ctrl REPL.2 0.9000000
10 b si REPL.2 0.9000000
11 c Ctrl REPL.2 0.6000000
12 c si REPL.2 0.5000000
> ggplot(summ, aes(x = SAMPLE, y = value, fill = SAMPLE)) +
geom_bar(position = position_dodge(), stat = "identity", color = "black") +
scale_fill_manual(values = c("#CCCCCC", "#FFFFFF")) +
geom_point(aes(shape = variable), size = 3) + facet_grid(~TARGET)
> ggplot(summ, aes(x = TARGET, y = value, fill = SAMPLE)) +
geom_bar(position = position_dodge(), stat = "identity", color = "black") +
scale_fill_manual(values = c("#CCCCCC", "#FFFFFF")) +
geom_point(aes(shape = variable, group = SAMPLE), position = position_dodge(1), size = 3)
Shapes are indicated via integers, you can find their mappings in numerous places, e.g. here
For all data points shown as filled squares, you would use geom_jitter(data = dt), aes(....your stuff as above...), shape = 15)
It's a bit difficult to tell from your example, but I think you actually want the shape to encode something else, maybe replicates? Then you would proceed just as for any other aesthetics binding, i.e., you would add shape to your aes() entries for geom_jitter:
geom_jitter(data = dt,
aes(x = TARGET,
y = value,
shape = replicate, # or whatever else you want to bind it to
fill = factor(...) # note here that not all shapes have fills, some only have color (basically their outline) some only have fill and some have both, play around with that
))
Since you already seem to have used shape as an aesthetic, what did you not like about the result when it was not commented out?
If you want to use specific shapes, add + scale_shape_manual(values = c(15, 16, 17)) (or any other combination, the order will depend on the order of the factor you're binding the shape to in the jitter command, just like for colors etc.
As a side note I would argue that with the current size, there's almost no difference between the top and bottom plot because one can barely tell the shape types apart in the top plot.
I think I didn't explain properly my question, I don't have any problem choosing the shapes by replicates, the problem is that I want them sorted like in the second picture, with the shape by replicate but all the 3 replicates in the same x-axis value.
Something like this:
.
.
.
And not this:
.
.
.
I believe that may be controlled by geom_jitter(). So, doing this will align them at a single x-axis value:
geom_jitter(position=position_jitter(width=0.0), ...) +
...or:
geom_jitter(
data = dt,
aes(
x = TARGET,
y =value,
fill =factor(SAMPLE, levels=c("Ctrl", "si"))),
#shape = variable),
position = position_dodge(width = 0.0), stat = "identity")
For ordering in ggplot2, generally, for continuous variables, it is 'first come, first serve' (row order in your input data); for categorical, the order is controlled by the order of the factor levels.
Already tried that, but the problem with this is that I get all the 6 points between the grey and white bars, and I want 3 in the white bar and 3 in the grey one :/
just use geom_point() instead of jitter
I've tried as well, the result is the same
The same as what (you've described multiple outcomes above)? Please show:
- the command that you use that includes
geom_point()instead ofgeom_jitter() - the resulting plot from that exact command
- a description of what you do not like about the outcome
ggplot(summ, aes(x = TARGET, y = value, fill = factor(SAMPLE, levels=c("Ctrl", "si")))) +
geom_bar(position = position_dodge(), stat = "identity", colour = "black", width = .6) +
geom_errorbar(aes(ymin=value-sd, ymax=value+sd), width=.2,
position=position_dodge(.6)) + theme_minimal() +
scale_fill_manual(values = c("#CCCCCC", "#FFFFFF")) +
ggtitle("Your title") +
theme(axis.title.x = element_blank()) + ylab("Fold Change\n") +
#scale_x_discrete(c(""))) +
guides(fill = guide_legend(title = NULL)) +
geom_point(
data = dt,
aes(
x = TARGET,
y =value,
fill =factor(SAMPLE, levels=c("Ctrl", "si"))),
shape = variable,
position = position_dodge(width = 0.6), stat = "identity")
I reduced the number of targets to make it clearer:

free image hosting
What I want is to stack the values from the 4 cntrl replicates in the cntrl bar (grey) and the 4 si replicates in the si bar (white), like when I do not shape = variable:

free image hosting
Thank you!
Log in to answer this question.


Look up
scale_shape_manual()I already have tried, as well as scale_shape_identity, still nothing :(