R boxplot color not changing - r

I'm making a box-and-whisker plot in R (y-axis # of reads and x-axis of 4 discrete conditions). I'm trying to switch the order in which the discrete conditions appear and to change them from the default white fill to a color of my choosing using the code below. I can get the order to change, but the color continues to stay white. I also have no idea why R cuts off my plot.
library(ggplot2)
capture_data = read.csv("tcp_for_r_plots.csv")
p <- ggplot(capture_data, aes(x=Protocol, y=raw_reads)) + geom_boxplot()
p <- p + scale_x_discrete(limits=c("Standard","TD-60","TD-55","TD-50"))
p <- p + scale_fill_manual(values=c("#999999","#FFFF00","#33FFFF","#FF33CC"))
Attached is the output I keep getting - no color change.

fill color: You need to add the fill option to the geom_boxplot() function as shown below (instead of using the scale_fill_manual function):
+ geom_boxplot(fill=c("#999999","#FFFF00","#33FFFF","#FF33CC"))
Order: the order is based on the alphabetical order of the factor values (Protocol). One solution is to recode the factor levels into the the order you want before running the generating the plot.

p <- ggplot(capture_data, aes(x=Protocol, y=raw_reads, fill=Protocol)) +
geom_boxplot() +
scale_x_discrete(limits=c("Standard","TD-60","TD-55","TD-50")) +
scale_fill_manual(values=c("#999999","#FFFF00","#33FFFF","#FF33CC"))

Add the colors in "fill" argument in ggplot:
p <- ggplot(capture_data, aes(x=Protocol, y=raw_reads)) + geom_boxplot()
should be
p <- ggplot(capture_data, aes(x=Protocol, y=raw_reads, fill = Protocol)) + geom_boxplot()
For example,
ggplot(mtcars, aes(x= as.factor(cyl), y=mpg, fill=as.factor(cyl))) + geom_boxplot()
gives me

Related

add different colour to bar plot

I constructed the following plot using ggplot using the following code:
ggplot(data, aes(x=Variable, y=Value, fill=Yield.Type)) +
geom_bar(stat="identity", position="dodge")
I had two questions:
1) How do I change the colour of the bar: I want to colour the pink bar as white and blue bar as grey with black borders. If in the code, I use col="White",fill="White", it colours both of them with the same colour and also stacks them up on each other
2) For each bar, I have the standard error in separate vector
For pink bars, se1<-c(0.08,0.07,0.08,0.07)
For blue bars, se2<-c(0.07,0.1,0.06,0.06)
I wanted to know how to add this standard errors to resepctive batch
How do I add this to the bar?
Please provide data it is much easier to answer. Here I have created a new set.
I have included martin's answer for the color
If you have a se value per bar, your should directly add it inside your data frame and use
geom_errorbar(). Check documentation for more info.
.
Variable <- factor(c("VAr1","VAr1","Var2","Var2","Var3","Var3","VAr4","VAr4"))
Yield.Type <- factor(c('O','R','O','R','O','R','O','R'))
Value <- c(1,2,3,4,3,5,6,5)
se1<-c(0.08,0.07,0.08,0.07,0.1,0.06,0.1,1)
data <- data.frame(Variable,Yield.Type,Value,se1)
limits <- aes(ymax = Value + se1, ymin=Value - se1)
dodge <- position_dodge(width=0.8)
ggplot(data, aes(x=Variable, y=Value,fill=Yield.Type,colour=Yield.Type)) +
geom_bar(stat="identity", position="dodge")+
scale_color_manual(values=c("black","black")) +
scale_fill_manual(values=c("white", "grey"))+
geom_errorbar(limits, position=dodge,width=0.1)
First question: use scale_color_manual and scale_fill_manual (see add different colour to bar plot)
p <- ggplot(...)
p + scale_color_manual(values=c("white","black")) +
scale_fill_manual(values=c("white", "grey"))
p
Second qestion: Look here or R: ggplot2 barplot and error bar for help.

ggplot2 geom_bar color only one column

Consider a sample dataframe and the relative geom_bar plot
data = data.frame(method=LETTERS[sample(x=c(1,2,3),size=100,replace=T)],
x1=sample(x=c(1,2,3,4,5,6),size=100,replace=T),
x2=sample(x=c(1,2,3,4,5,6),size=100,replace=T),
d =letters[sample(c(1,2,3,4),size=100,replace=T)] )
ggplot()+
geom_bar(data=data, aes(x=method, y=x1),stat="identity") +
facet_wrap(~d, ncol=2)
I would like to color the smaller column of each plot of red.
How can I do that?
I'm not sure how you would do it without collapsing your data to be able to create a new column which specifies which value is the minimum. Then you can attach an aesthetic to that value. Here's a collapsing strategy using your data
collapsed < -as.data.frame(xtabs(x1~d+method, data))
collapsed$ismin <- with(collapsed, ave(Freq,d,FUN=function(x) x==min(x)))
And now we plot with
ggplot(collapsed, aes(x=method, y=Freq, fill=as.factor(ismin)))+
geom_bar(stat="identity") +
facet_wrap(~d, ncol=2) +
scale_fill_manual(breaks=c("0","1"), values=c("black","red"), guide="none")
which results in

ggplot2: How to summarize count? stat_summary or stat_bin

I'm using ggplot2 to just count and summarize the number of occurrences of each mode in my data frame. testdata$V5 is a factor with 4 different modes. Every line in testdata has an entry for mode and I want to count them.
p <- ggplot(testdata,aes(V5))
p = p + geom_histogram()
show(p)
This code produces the following plot:
I am now trying to show text labels on top of each bar plot that show the count but I can't quite understand how to achieve that using stat_summary. How can I produce a text label at the top of each x value bar showing the count?
I tried
p <- ggplot(testdata,aes(V5))
p = p + geom_histogram()
p = p + stat_summary(fun.data=count, geom="text", size=20, color="red") #<-- no effect
show(p)
but it doesn't draw anything.
You can use a "hidden" variable ..count.. in conjunction with geom_text:
p +
geom_histogram() +
stat_bin(aes(label=..count..), geom="text", position="identity", size=20, color="red")
geom_text also has hjust and vjust parameters that may be helpful.

Plotting continuous and discrete series in ggplot with facet

I have data that plots over time with four different variables. I would like to combine them in one plot using facet_grid, where each variable gets its own sub-plot. The following code resembles my data and the way I'm presenting it:
require(ggplot2)
require(reshape2)
subm <- melt(economics, id='date', c('psavert','uempmed','unemploy'))
mcsm <- melt(data.frame(date=economics$date, q=quarters(economics$date)), id='date')
mcsm$value <- factor(mcsm$value)
ggplot(subm, aes(date, value, col=variable, group=1)) + geom_line() +
facet_grid(variable~., scale='free_y') +
geom_step(data=mcsm, aes(date, value)) +
scale_y_discrete(breaks=levels(mcsm$value))
If I leave out scale_y_discrete, R complains that I'm trying to combine discrete value with continuous scale. If I include scale_y_discreate my continuous series miss their scale.
Is there any neat way of solving this issue ie. getting all scales correct ? I also see that the legend is alphabetically sorted, can I change that so the legend is ordered in the same order as the sub-plots ?
Problem with your data is that that for data frame subm value is numeric (continuous) but for the mcsm value is factor (discrete). You can't use the same scale for numeric and continuous values and you get y values only for the last facet (discrete). Also it is not possible to use two scale_y...() functions in one plot.
My approach would be to make mcsm value as numeric (saved as value2) and then use them - it will plot quarters as 1,2,3 and 4. To solve the problem with legend, use scale_color_discrete() and provide breaks= in order you need.
mcsm$value2<-as.numeric(mcsm$value)
ggplot(subm, aes(date, value, col=variable, group=1)) + geom_line()+
facet_grid(variable~., scale='free_y') + geom_step(data=mcsm, aes(date, value2)) +
scale_color_discrete(breaks=c('psavert','uempmed','unemploy','q'))
UPDATE - solution using grobs
Another approach is to use grobs and library gridExtra to plot your data as separate plots.
First, save plot with all legends and data (code as above) as object p. Then with functions ggplot_build() and ggplot_gtable() save plot as grob object gp. Extract from gp only part that plots legend (saved as object gp.leg) - in this case is list element number 17.
library(gridExtra)
p<-ggplot(subm, aes(date, value, col=variable, group=1)) + geom_line()+
facet_grid(variable~., scale='free_y') + geom_step(data=mcsm, aes(date, value2)) +
scale_color_discrete(breaks=c('psavert','uempmed','unemploy','q'))
gp<-ggplot_gtable(ggplot_build(p))
gp.leg<-gp$grobs[[17]]
Make two new plot p1 and p2 - first plots data of subm and second only data of mcsm. Use scale_color_manual() to set colors the same as used for plot p. For the first plot remove x axis title, texts and ticks and with plot.margin= set lower margin to negative number. For the second plot change upper margin to negative number. faced_grid() should be used for both plots to get faceted look.
p1 <- ggplot(subm, aes(date, value, col=variable, group=1)) + geom_line()+
facet_grid(variable~., scale='free_y')+
theme(plot.margin = unit(c(0.5,0.5,-0.25,0.5), "lines"),
axis.text.x=element_blank(),
axis.title.x=element_blank(),
axis.ticks.x=element_blank())+
scale_color_manual(values=c("#F8766D","#00BFC4","#C77CFF"),guide="none")
p2 <- ggplot(data=mcsm, aes(date, value,group=1,col=variable)) + geom_step() +
facet_grid(variable~., scale='free_y')+
theme(plot.margin = unit(c(-0.25,0.5,0.5,0.5), "lines"))+ylab("")+
scale_color_manual(values="#7CAE00",guide="none")
Save both plots p1 and p2 as grob objects and then set for both plots the same widths.
gp1 <- ggplot_gtable(ggplot_build(p1))
gp2 <- ggplot_gtable(ggplot_build(p2))
maxWidth = grid::unit.pmax(gp1$widths[2:3],gp2$widths[2:3])
gp1$widths[2:3] <- as.list(maxWidth)
gp2$widths[2:3] <- as.list(maxWidth)
With functions grid.arrange() and arrangeGrob() arrange both plots and legend in one plot.
grid.arrange(arrangeGrob(arrangeGrob(gp1,gp2,heights=c(3/4,1/4),ncol=1),
gp.leg,widths=c(7/8,1/8),ncol=2))

ggplot column chart - order of colours in brewer object has no effect

I am trying to change the colours on a ggplot column chart. After googling, I thought that the following code would work:
require(ggplot2)
require(RColorBrewer)
State <- c(rep("NSWTC",5), rep("TCV",5), rep("QTC",5),
rep("WATC",5), rep("SAFA",5), rep("Other",5))
Year <- rep(c("11-12","12-13","13-14","14-15","15-16"),6)
##some random data
Funding.Programme <- abs(rnorm(30))
df <- data.frame(State, Year, Funding.Programme)
##this line makes the graph in the order you inputted it, rather than alphabetical
df$State <- factor(df$State, levels=unique(df$State))
##ugly coloured
bars <- ggplot(df) +
aes(x=Year ,y=Funding.Programme, fill=Year) +
geom_bar(stat='identity') +
facet_grid(facets=~State) +
scale_y_continuous('Gross Issuance Requirements')
##nicely coloured
blues <- brewer.pal(5, "Blues")
blues <- rev(blues)
##the following two graphs have the same colours
bars <- ggplot(df) +
aes(x=Year ,y=Funding.Programme, fill=Year) +
geom_bar(stat='identity') +
facet_grid(facets=~State) +
scale_y_continuous('Gross Issuance Requirements') +
scale_fill_brewer(blues)
bars
bars <- ggplot(df) +
aes(x=Year ,y=Funding.Programme, fill=Year) +
geom_bar(stat='identity') +
facet_grid(facets=~State) +
scale_y_continuous('Gross Issuance Requirements') +
scale_fill_brewer(blues.rev)
bars
##and this does not adjust the default colours
bars <- ggplot(df)+
aes(x=Year,y=Funding.Programme, fill=Year) +
geom_bar(stat='identity') +
facet_grid(facets=~State) +
scale_y_continuous('Gross Issuance Requirements') +
scale_colour_manual(values = blues.rev)
bars
But the last method does not work, and the second and third-last charts produced are identical, despite the order of colours being reversed in the object.
You want scale_fill_manual(values = blues) or conversely with blues.rev (which you didn't actually create in your example code, which I assume is a typo).
Only use scale_*_brewer when you're selecting one of the default palette's by name. Otherwise, use scale_*_manual for this sort of thing.
The last one doesn't work because you were using colour instead of fill.
Finally, carriage returns and tabs: love them, cherish them, use them!

Resources