colored 100% barplot with ggplot2 - r

Ia am completly new to R and just absolved the introduction from edX. Unfortunately it doesn't teach anything about ggplot2, what i want to use for my graphs in my thesis. I want to get 100% bar plots with different colours and if possible the value in the according bar. my data looks like this:
Concentration,Percentage,Phenotype
Control,0.933333333,0
Control,0.014814815,1
Control,0.022222222,2
Control,0.02962963,3
0.002,0.918181818,0
0.002,0.018181818,1
0.002,0.018181818,2
0.002,0.045454545,3
0.02,0.930434783,0
0.02,0.017391304,1
0.02,0.017391304,2
0.02,0.034782609,3
0.2,0.928571429,0
0.2,0.032467532,1
0.2,0.012987013,2
0.2,0.025974026,3
2,0.859813084,0
2,0.028037383,1
2,0.046728972,2
2,0.065420561,3
and the code i used is this:
ggplot(Inj, aes(x=Concentration, y=Percentage, fill=Phenotype))+geom_bar(stat='identity',color='black')
The resulting graph looks like that:
how can i change the color of the different bars and get the %-values in the bars?
Thanks for any help

You can make the colors controllable by making your fill variable a factor. Then you can manually adjust the colors like this
ggplot(Inj, aes(x=Concentration, y=Percentage, fill=factor(Phenotype)))+geom_bar(stat='identity',color='black') +
scale_fill_manual(values=c("red", "blue", "yellow", "pink"))
I don't recommend putting the values over the colors as they won't be visible for the very small values. I would use another way to visualize the data.

You can use scale_x_discrete to re-order the factors manually like this:
ggplot(Inj, aes(x=Concentration, y=Percentage, fill=Phenotype)) +
geom_bar(stat='identity',color='black') +
scale_fill_grey(start = .4, end = .9) +
theme_bw()+ylab("Distribution") +
xlab("Contentration [mg/ml]") +
ggtitle("Dose-respond analysis") +
theme(legend.title = element_text(colour="black", size=10, face="bold")) +
theme(legend.background = element_rect(fill="white",
size=0.5, linetype="solid",
colour ="black")) +
scale_x_discrete(limits=c('Control','0.002', '0.02', '0.2', '2'),
labels=c('Control\n(N=000)',
'0.002\n(N=000)',
'0.02\n(N=000)',
'0.2\n(N=000)',
'2\n(N=000)'))
I added the labels part to scale_x_discrete in response to your question in the comments below. You just need to update the N values. Notice that I put \n in each label in order to force the label text on to two lines so it wouldn't be jumbled. Just remove the \n if you'd prefer it to be on a single line.

Related

Removing the original color outline in R when using a new pallette in a barplot

I am using plot_bar function in R to illustrate some data from a phyloseq class. I want to add some different colors to my plot, here the pallette Paired from the RColorBrewer package. For some reasons the bars end up still having the default color around them. Here you can see how it looks like: https://imgur.com/a/VSBvwtk
Any way I could get rid of them?
phylum.both.b <- plot_bar(both.b, fill="phylum") +
geom_bar(aes(color=phylum, fill=phylum), stat="identity") +
scale_y_continuous(expand=c(0,0)) +
labs(x="", y="Relative abundance") +
scale_fill_brewer(palette="Paired") +
theme(axis.text.x=element_blank()) +
facet_wrap(~Type, scale="free_x", ncol=4) +
facet_row(vars(Type), scales = 'free', space = 'free')
ggsave("PhylumBothBact.png", phylum.both.b, height=15, width=40, unit="cm")
You defined color = phylum in the creation of your plot, but never manually defined the color, so the default is still used. fill fills the bars with color in a bar plot and color outlines the bars.
Try adding scale_color_manual(values = NA)to your plot. Alternatively if you want the outline to match you could use scale_color_brewer(palette = "Paired")

Adding a legend to a combined line and bargraph ggplot

So I know many people have asked similar questions but the code others have used does not seem to be working for my graph hence why I'm wondering if I have done something wrong.
I have this code:
ggplot(dfMonth)
+ geom_col(aes(x=Month, y=NumberMO), size=.7, colour="black", fill="white")
+ geom_line(aes(x=Month, y=NumberME), size=1, colour="black", group=1)
+ xlab("Month")
+ ylab("No. of birds observed")
+ theme_bw()
+ geom_point(x=Month, y=NumberME)
+ scale_colour_manual("" ,values =c("NumberME"="black"), labels=c("Expected No. of birds"))
+ theme(legend.key=element_blank(),legend.title=element_blank(), legend.box="horizontal")
+ theme(axis.title.x = element_text(margin = unit(c(5, 0, 0, 0), "mm")),
axis.title.y = element_text(margin = unit(c(0,3 , 0, 0), "mm")))
Which produces this graph:
so as you can see, the legend to show what the black line with the points mean has not been added to my graph even though I have inputted the code. No error comes up so hence why I'm lost on whats wrong. Any ideas on what i've failed to include?
Thanks
In order for ggplot to know to draw a legend, you need to include one of the aesthetics for a geom within aes(). In this case, if you want a legend to be drawn for your line, you need to include within the aes() in the geom_line() call one of the aesthetics that you have identified for the line: linetype or color works. We'll use color here.
Oh... and in the absence of OP sharing their dataset, here's a made-up example:
set.seed(1234)
dfMonth <- data.frame(
Month=month.name,
NumberMO=sample(50:380, 12),
NumberME=sample(50:380, 12)
)
Now the code to make the plot and ensure the legend is created.
p <- ggplot(dfMonth, aes(x=Month)) +
geom_col(aes(y=NumberMO), size=0.7, color="black", fill="white") +
geom_line(aes(y=NumberME, color='black'), size=1, group=1)
p
We have a legend, but there's some problems. You get the default title of the legend (which is the name of the aesthetic), and the default label (which is whatever text you put inside aes(color=.... Since we put "black" as the value there, it's applied as the label, and not the actual color. The actual color of the line is to default to the first level of the standard colorset used by ggplot2, which in this case is that light red color.
To set the color, name of the legend, and name of the label, we should specify the value. There's only one item in the legend, so there's no need to specify, but if you were to send a named vector to indicate the name for our single line explicitly, you end up with the somewhat strange-looking c('black'='black'). I also included a line break in the label name to make the look a bit better. Also, the months were running into each other, so I also changed the angle of the x axis labels.
Finally, you might notice the months were out of order. That's because default ggplot2 behavior is to factor a column of discrete values, which uses alphabetical ordering for the levels. To fix that, you specify the column as a factor before plotting with the correct levels.
dfMonth$Month <- factor(dfMonth$Month, levels=month.name)
p + scale_color_manual(
name=NULL, values=c('black'='black'),
labels='Expected No.\nof birds') +
theme(axis.text.x=element_text(angle=30, hjust=1))

Custom legend in ggplot2: how to fill without factors?

I am making a personality survey that will generate score reports for participants. I want to make these as easy to read and understand as possible, so I am generating a normal curve for the surveyed trait and a line showing the person where they fall on the curve.
First, let's generate some data:
sample <- as.data.frame(rnorm(1000, 0, 1))
names(sample) <- "trait"
score <- mean(sample$trait)
My problem is with the legend—I cannot figure out how to customize the legend to display 1) a filled "population" color when I'm not graphing multiple factors, and 2) the line showing the participant's score.
I can get close:
ggplot(sample, aes(x=trait)) +
geom_density(fill="blue") +
geom_density(aes(fill="Population")) +
geom_vline(aes(xintercept=score, color="You")) +
geom_vline(xintercept=score, color='red',
linetype="solid",size=1.5) +
scale_colour_manual(values=c('Population'='blue',
'You'='red'))
Graph image 1
But this does not use the colors specified, and has extraneous "colour" and "fill" text in the legend.
If I change the geom_density aesthetic to color instead of fill and leave everything else the same...
geom_density(aes(color="Population")) +
...This correctly applies the colors, but then does not fill the "Population" box in the legend with blue.
Graph image 2
Optimally, I'd like to fill the "Population" box blue and remove the red box around the "You" line in the legend. How can I achieve this?
I hope this can be used.
ggplot(sample, aes(trait)) +
geom_density(aes(fill = "Population")) +
geom_vline(aes(xintercept = mean(trait), color = "You")) +
theme(legend.title = element_blank()) +
scale_color_manual(values = "red", breaks = "You") +
scale_fill_manual(values = "blue", breaks = "Population")

R: tailoring legend in ggplot boxplot leaves two separate legends

I have created a faceted boxplot using the ggplot2 package. The R code is as follows:
version.labs <- c(`1`="Version 1.0", `2`="Version 2.0", `3`="Version 3.0", `4`="Version 4.0", `5`="Version 5.0")
ggplot(df, aes(x=factor(Subsystem), y=Risk.value, fill=factor(Version)) ) +
geom_jitter(position=position_jitter(width=0.3, height=0.2), aes(colour=factor(Version)), alpha=0.9) +
geom_boxplot(alpha = 0.5, show.legend = FALSE) + facet_grid(.~Version, labeller = as_labeller(version.labs)) +
theme(strip.text.x = element_text(size=9, color="black", face="bold"))
The resulting plot looks pretty good (as shown below) exept for the legend.
In the legend I want to change the title as well as the text label for each item. The title should be "Version" and the labels "Version 1.0", ..., "Version 5.0".
I've tried various ways but they all add a new separate legend. The new legend looks good, but the old one is still there which doesn't look good and I can't find a way to remove it.
The last thing I tried was to add the scale_color_manual() function, like this:
scale_color_manual(name = "Version", labels=c("v1.0","v2.0","v3.0","v4.0","v5.0"), values=c("grey","blue","green","red","black"))
This results in a boxplot that looks like this.
As can be seen there are two legends. So, close but no cigar. Any hints on how to solve this are appreciated.
I figured out the problem. It was that I had placed my aestetic fill function aes() in the general ggplot(). This should instead be placed in geom_boxplot(), otherwise both the general ggplot() as well as the geom_boxplot() will add a legend. So I fixed that, and then I used guides() to update the title in the geom_boxplot() legend. The full code looks as follows:
ggplot(df, aes(x=factor(Subsystem), y=Risk.value) ) +
geom_jitter(position=position_jitter(width=0.3, height=0.2), aes(colour=factor(Version)), alpha=0.9) +
geom_boxplot(alpha = 0.5, show.legend = FALSE, aes(fill=factor(Version))) + facet_grid(.~Version, labeller = as_labeller(version.labs)) +
theme(strip.text.x = element_text(size=9, color="black", face="bold")) +
labs(x="Risk distribution per software version and subsystem type", y="Normalized risk value") +
guides(color=guide_legend("Version"))
The final plot looks like this, which I'm happy with.
You are using fill argument for grouping and generation of legend. may be instead of scale_color_manual you can use scale_fill_manual to override the existing legend

How can I remove the legend title in ggplot2?

I have a question concerning the legend in ggplot2.
Say I have a hypothetical dataset about mean carrot length for two different colours at two farms:
carrots<-NULL
carrots$Farm<-rep(c("X","Y"),2)
carrots$Type<-rep(c("Orange","Purple"),each=2)
carrots$MeanLength<-c(10,6,4,2)
carrots<-data.frame(carrots)
I make a simple bar plot:
require(ggplot2)
p<-ggplot(carrots,aes(y=MeanLength,x=Farm,fill=Type)) +
geom_bar(position="dodge") +
opts(legend.position="top")
p
My question is: is there a way to remove the title ('Type') from the legend?
Thanks!
I found that the best option is to use + theme(legend.title = element_blank()) as user "gkcn" noted.
For me (on 03/26/15) using the previously suggested labs(fill="") and scale_fill_discrete("") remove one title, only to add in another legend, which is not useful.
You can modify the legend title by passing it as the first parameter to a scale. For example:
ggplot(carrots, aes(y=MeanLength, x=Farm, fill=Type)) +
geom_bar(position="dodge") +
theme(legend.position="top", legend.direction="horizontal") +
scale_fill_discrete("")
There is also a shortcut for this, i.e. labs(fill="")
Since your legend is at the top of the chart, you may also wish to modify the legend orientation. You can do this using opts(legend.direction="horizontal").
You can use labs:
p + labs(fill="")
The only way worked for me was using legend.title = theme_blank() and I think it is the most convenient variant in comparison to labs(fill="") and scale_fill_discrete(""), which also could be useful in some cases.
ggplot(carrots,aes(y=MeanLength,x=Farm,fill=Type)) +
geom_bar(position="dodge") +
opts(
legend.position="top",
legend.direction="horizontal",
legend.title = theme_blank()
)
P.S. There are more useful options in documentation.
You've got two good options already, so here's another using scale_fill_manual(). Note this also lets you specify the colors of the bars easily:
ggplot(carrots,aes(y=MeanLength,x=Farm,fill=Type)) +
geom_bar(position="dodge") +
opts(legend.position="top") +
scale_fill_manual(name = "", values = c("Orange" = "orange", "Purple" = "purple"))
If you are using the up-to-date (As of January 2015) version of ggplot2 (version 1.0), then the following should work:
ggplot(carrots, aes(y = MeanLength, x = Farm, fill = Type)) +
geom_bar(stat = "identity", position = "dodge") +
theme(legend.position="top") +
scale_fill_manual(name = "", values = c("Orange" = "orange", "Purple" = "purple"))
#pascal 's solution in a comment to set the name argument of a scale function, such as scale_fill_discrete, to NULL, is the best option for me. It allows removing the title together with the blank space that would remain if you used "", while at the same time allowing the user to selectively remove titles, which is not possible with the theme(legend.title = element_blank()) approach.
Since it is buried in a comment, I am posting it as an answer to potentially increase its visibility, with kudos to #pascal.
TL;DR (for the copy-pasters):
scale_fill_discrete(name = NULL)

Resources