I have a plot in which I am displaying individual values from multiple subjects, coloured by group. Added to that are means per group, calculated using stat_summary.
I would like the two means to be coloured by group, but in colours other than the individual data. This turns out to be difficult, at least when using stat_summary. I have the following code:
ggplot(data=dat,
aes(x=Round, y=DV, group=Subject, colour=T1)) +
geom_line() + geom_point() + theme_bw() +
stat_summary(fun.y=mean, geom="line", size=1.5,
linetype="dotted", color="black",
aes(group=T1))
Which produces this example graph.
The colour for the means created by stat_summary is set to black; otherwise it would be red and blue like the individual data lines. However, it is not possible to set more than one colour - so color=c("black", "blue") does not work.
I've already tried scale_colour_manual as explained here, but this will change the colours of the individual data lines, leaving the mean lines unaffected.
Any suggestion how to solve this? Code and data here.
You need to create different values for the mapping to color:
ggplot(data=iris,
aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
geom_line() + geom_point() + theme_bw() +
stat_summary(fun.y=mean, geom="line", size=1.5,
linetype="dotted", aes(color=paste("mean", Species)))
You can then use scale_color_manual to get specific colors.
Related
I have several geometric functions, specifically geom_point and geom_abline. I want some of them to have the same color by mapping the color aesthetic to custom values (that are not defined by a column in the dataframe).
In the example below, I want the lines to have to the same color as the surrounding points (without setting color='red' outside of aes() or adding a grouping column to the original data).
library(tidyverse)
ggplot(mtcars) +
geom_point(aes(x=disp, y=wt, color='wt')) +
geom_point(aes(x=disp, y=qsec, color='qsec')) +
geom_abline(intercept=0, slope=0.01, aes(color='wt')) +
geom_abline(intercept=20, slope=-0.01, aes(color='qsec'))
How do I do that?
As discussed here
The way to do this is
library(tidyverse)
ggplot(mtcars) +
geom_point(aes(x=disp, y=wt, color='wt')) +
geom_point(aes(x=disp, y=qsec, color='qsec')) +
geom_abline(aes(intercept=b, slope=m, color='wt'), data.frame(b=0, m=0.01)) +
geom_abline(aes(intercept=b, slope=m, color='qsec'), data.frame(b=20, m=-0.01))
I want to add a legend for filled rectangles in the background but I already used fill aesthetics for filling the bars of my bar plot.
How can I get the legend or create a matching legend by hand?
df <- data.frame(a=factor(c('a','b','c','c','d','e'), levels=c('a','b','c','d','e')),
x=seq(1,6),
b=factor(c('A','A','A','B','B','B'), levels=c('A','B')),
c=c(1,2,3,4,5,6),
d=rnorm(6))
ggplot(df, aes(x, c, fill=d, group=b)) +
geom_rect(aes(xmin=0.5,xmax=3.5,ymin=-Inf,ymax=Inf),alpha=0.05,fill="#E41A1C") +
geom_rect(aes(xmin=3.5,xmax=6.5,ymin=-Inf,ymax=Inf),alpha=0.05,fill="#377EB8") +
geom_bar(stat='identity', position=position_dodge()) +
coord_flip() +
scale_x_continuous(breaks=df$x, labels=df$a)
So I need a legend describing my two geom_rect areas. I was not able to map my two areas in any way to get a legend. In general the column df$b is describing the areas I do now by hand.
You can set colour= to variable b inside the aes() of both geom_rect(). This will make lines around the rectangles and also make legend. Lines can be removed setting size=0 for geom_rect(). Now using guides() and override.aes= you can change fill= for legend key.
ggplot(df, aes(x, c, fill=d, group=b)) +
geom_rect(aes(xmin=0.5,xmax=3.5,ymin=-Inf,ymax=Inf,colour=b),alpha=0.05,fill="#E41A1C",size=0) +
geom_rect(aes(xmin=3.5,xmax=6.5,ymin=-Inf,ymax=Inf,colour=b),alpha=0.05,fill="#377EB8",size=0) +
geom_bar(stat='identity', position=position_dodge()) +
coord_flip() +
scale_x_continuous(breaks=df$x, labels=df$a)+
guides(colour=guide_legend(override.aes=list(fill=c("#E41A1C","#377EB8"),alpha=0.3)))
I have a plot like the one below created using ggplot2, which has three facet panels.
However, I would like to have, say two shades of red in the left panel, two shades of green in the middle panel, and two shades of blue in the right panel. So in other words, the two ribbons in each panel differ in their colors, and the colors between panels also differ. But I haven't figured out a way to do it. The code I used to obtain the code is below. factor1 is a 2-level variable, and factor2 is a 3-level variable. Thanks in advance for your help!
ggplot(dataset, aes(x = window, group=factor1)) +
facet_grid(.~factor2) +
geom_line(aes(y=fit), alpha=0.8, colour="gray20") +
geom_ribbon(aes(ymin=fit - 1.96*se, ymax=fit+1.96*se, fill=factor1), alpha=0.7) +
geom_vline(xintercept=0, colour="gray20") +
geom_vline(xintercept=4, colour="red", size=.1) +
geom_hline(xintercept=0, colour="black",size=.1, linetype="dashed") +
theme(panel.background=element_rect(fill="white", colour="black")) +
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank())
You can specify variable interactions to vary colors. You need to define the color palette manually to get the shading you want:
ggplot(dataset, aes(x = window, group=factor1)) +
facet_grid(.~factor2) +
geom_line(aes(y=fit), alpha=0.8, colour="gray20") +
geom_ribbon(aes(ymin=fit - 1.96*se, ymax=fit+1.96*se, fill=factor1:factor2), alpha=0.7) +
geom_vline(xintercept=0, colour="gray20") +
geom_vline(xintercept=4, colour="red", size=.1) +
geom_hline(xintercept=0, colour="black",size=.1, linetype="dashed") +
theme(panel.background=element_rect(fill="white", colour="black")) +
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank())
I am trying to plot the outliers and mean point for the box plots in below using the data available here. The dataset has 3 different factors and 1 value column for 3600 rows.
While I run the below the code it shows the mean point but doesn't draw the outliers properly
ggplot(df, aes(x=Representations, y=Values, fill=Methods)) +
geom_boxplot() +
facet_wrap(~Metrics) +
stat_summary(fun.y=mean, colour="black", geom="point", position=position_dodge(width=0.75)) +
geom_point() +
theme_bw()
Again, while I am modify the code like in below the mean points disappear !!
ggplot(df, aes(x=Representations, y=Values, colour=Methods)) +
geom_boxplot() +
facet_wrap(~Metrics) +
stat_summary(fun.y=mean, colour="black", geom="point", position=position_dodge(width=0.75)) +
geom_point() +
theme_bw()
In both of the cases I am getting the message: "ymax not defined: adjusting position using y instead" 3 times.
Any kind suggestions how to fix it? I would like to draw the mean points within individual box plots and show outliers in the same colour as the plots.
EDIT:
The original data set does not have any outliers and that was reason for my confusion. Thanks to MrFlick's answer with randomly generated data which clarifies it properly.
Rather than downloading the data, I just made a random sample.
set.seed(18)
gg <- expand.grid (
Methods=c("BC","FD","FDFND","NC"),
Metrics=c("DM","DTI","LB"),
Representations=c("CHG","QR","HQR")
)
df <- data.frame(
gg,
Values=rnorm(nrow(gg)*50)
)
Then you should be able to create the plot you want with
library(ggplot2)
ggplot(df, aes(x=Representations, y=Values, fill=Methods)) +
geom_boxplot() +
stat_summary(fun.y="mean", geom="point",
position=position_dodge(width=0.75), color="white") +
facet_wrap(~Metrics)
which gave me
I was using ggplot2 version 0.9.3.1
I am new to ggplot2. I would like to create a line plot that has points on them where the points are filled with different colors than the lines (see the plot below).
Suppose the dataset I am working with is the one below:
set.seed(100)
data<-data.frame(dv=c(rnorm(30), rnorm(30, mean=1), rnorm(30, mean=2)),
iv=rep(1:30, 3),
group=rep(letters[1:3], each=30))
I tried the following code:
p<-ggplot(data, aes(x=iv, y=dv, group=group, pch=group)) + geom_line() + geom_point()
p + scale_color_manual(values=rep("black",3))+ scale_shape(c(19,20,21)) +
scale_fill_manual(values=c("blue", "red","gray"))
p + scale_shape(c(19,20,21)) + scale_fill_manual(values=c("blue", "red","gray"))
But I do not get what I want.I hope someone can point me to the right direction. Thanks!
scale_fill_manual(), scale_shape_manual() and scale_colour_manual() can be used only if you have set fill=, shape= or colour= inside the aes().
To change colour just for the points you should add colour=group inside geom_point() call.
ggplot(data, aes(x=iv, y=dv, group=group,shape=group)) +
geom_line() + geom_point(aes(colour=group)) +
scale_shape_manual(values=c(19,20,21))+
scale_colour_manual(values=c("blue", "red","gray"))