R boxplot how is this manual fill wrong - r

I would like to colour my boxplot variables differently. I looked here and tried the following but the plot boxes are all the standard white colour (i have 6 factors in Type). What should i change?
library(ggplot2)
ggplot(PGcounts, aes(Type, Word)) +
geom_boxplot() +
coord_trans(y = "log10") +
scale_fill_manual(values = c("white","white","white","red","blue","white"))

Also you can just change from geom_boxplot() to geom_boxplot(aes(fill=Type)) in you original codes.
ex:
ggplot(PGcounts, aes(Type, Word)) +
geom_boxplot(aes(fill=Type)) +
coord_trans(y = "log10") +
scale_fill_manual(values = c("white","white","white","red","blue","white"))

What has to change is
geom_boxplot() +
to
geom_boxplot(fill = c("white","white","white","red","blue","white")) +
and remove
scale_fill_manual(values = c("white","white","white","red","blue","white"))

Related

How to flip a geom_area to be under the line when using scale_y_reverse()

I had to flip the axis of my line, but still need the geom_area to be under the curve. However I cannot figure out how to do so.
This is the line of code I tried
ggplot(PalmBeachWell, aes(x=Date, y=Depth.to.Water.Below.Land.Surface.in.ft.)) +
geom_area(position= "identity", fill='lightblue') +
theme_classic() +
geom_line(color="blue") +
scale_y_reverse()
and here is what i got
One option would be to use a geom_ribbon to fill the area above the curve which after applying scale_y_reverse will result in a fill under the curve.
Using some fake example data based on the ggplot2::economics dataset:
library(ggplot2)
PalmBeachWell <- economics[c("date", "psavert")]
names(PalmBeachWell) <- c("Date", "Depth.to.Water.Below.Land.Surface.in.ft.")
ggplot(PalmBeachWell, aes(x = Date, y = Depth.to.Water.Below.Land.Surface.in.ft.)) +
geom_ribbon(aes(ymin = Depth.to.Water.Below.Land.Surface.in.ft., ymax = Inf),
fill = "lightblue"
) +
geom_line(color = "blue") +
scale_y_reverse() +
theme_classic()

ggplo2 sample size annotation: How do you place the sample size under the x-axis?

I tried with the "solutions" from other posts without success. So, I am trying to add to my ggplot2 barplot the sample size per group under the names at the x-axis. I am using the stat_n_text(), but it does not allow you to change the position outside the chart. Does anyone know how to do this, or is there any other approach to adding the sample size per group?
Here is my code and my output.
ggplot(data, aes(group, pm_l, fill=condition)) + theme_classic() +
geom_bar(position = position_dodge(), stat = "identity") +
facet_wrap(~parameter, scales = "free") +
ylab("pm_l") +
scale_fill_brewer(palette = "Paired") +
stat_n_text(y.pos = 0)
I want the n=X to be named MICRO and ONE in each plot.
Any help or suggestions are highly appreciated!!!!
Maybe you can try something like this. As we don't have the data, this is just an example:
library(ggplot2)
df <- data.frame(ColA = c("MICRO","MICRO","MICRO","ONE","ONE","ONE"),
ColB = c(6,-5,9,-2,2,-1),
group = c("FLUX","ROXn","ROXn","FLUX","ROXn","ROXn"),
condition = c("PRE","POST","PRE","POST","PRE","POST"))
ggplot(df,aes(ColA,ColB,fill = condition, color = condition)) +
geom_bar(stat="identity") +
facet_wrap(~group) +
theme_minimal() +
ylab("pm_l") +
xlab("group") +
scale_fill_brewer(palette = "Paired")
OUTPUT:

Bar plot options with ggplot2

I am actually trying to do a graph with ggplot2 but I'd like to add some options (colors, legend...).
Here is my code :
ggplot(FINAL, aes(x = as.factor(gender), y=overtreatment)) +
stat_summary(fun.y="mean", geom="bar") +
facet_grid(. ~ treatment) +
theme_grey() +
xlab("Treatment") +
ylab("OT") +
scale_fill_grey() +
theme(strip.background = element_rect(colour = "black", fill = "white"))
And here the actual output.
Could you please indicate me how to change the name of 1 and 2 (without changing in it the dataframe) and how to add colours to this ?
I tried this
ggplot(FINAL, aes(x = as.factor(gender), y=overtreatment, colour=Treatment))
But it applies the color only to the outline of the figure.
To change the color of the bars you need fill = Treatment.
To change the labels on the x axis you need scale_x_discrete(labels = your_labels). See here.
So your code will look like:
ggplot(FINAL, aes(x = as.factor(gender), y=overtreatment, fill= Treatment)) +
scale_x_discrete(labels = your_labels) +
...

R - setting manual color based on conditional value

I'm trying to assign a color to a ggplot bar graph based on whether the value is above or below 0.5.
Here is reproducible code below and graph without the color assigned.
dnow <- data.frame(x=rep(c("protected areas","wildnerness areas","private lands","multi-use lands"), each=25), y=runif(100))
ggplot(dnow,aes(x=x, y=y)) + stat_summary(fun.y=mean, geom="bar", position=position_dodge(1)) +
stat_summary(fun.data = mean_se,geom="errorbar", color="grey40",position=position_dodge(1), width=.2) +
geom_hline(yintercept = 0.5) + labs(y="Mean Agreement") + theme_bw() +
theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(), axis.title.y=element_blank()) +
coord_flip()
Based on the following stackoverflow question (Setting a conditional color by stat_summary in ggplot) I tried to manually assign colors based on the threshold value of 0.5 using aes in the stat_summary so that bars with values over 0.5 are green and bars with values under 0.5 are red.
The code and output are below. The graph however does not look correct. It created two bars with a "true" or "false" instead of coloring the single bar based on the threshold value. Not sure how to resolve this.
ggplot(dnow,aes(x=x, y=y)) + stat_summary(fun.y=mean, geom="bar", aes(fill = y > 0.5), position=position_dodge(1)) +
stat_summary(fun.data = mean_se,geom="errorbar", color="grey40",position=position_dodge(1), width=.2) +
geom_hline(yintercept = 0.5) + labs(y="Mean Agreement") + theme_bw() +
theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(), axis.title.y=element_blank()) +
scale_fill_manual(values = c('red', 'green')) + coord_flip()
You could simply change your y to ..y.. in your aes. Although it is probably best to aggregate your data before hand and use geom_bar similar to the post you linked. This should work:
ggplot(dnow,aes(x=x, y=y)) + stat_summary(fun.y=mean, geom="bar", aes(fill = ..y.. > 0.5), position=position_dodge(1)) +
stat_summary(fun.data = mean_se,geom="errorbar", color="grey40",position=position_dodge(1), width=.2) +
geom_hline(yintercept = 0.5) + labs(y="Mean Agreement") + theme_bw() +
theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(), axis.title.y=element_blank()) +
scale_fill_manual(values = c('red', 'green')) + coord_flip()
..y.. refers to the computed mean from fun.y.
The reason is that you use the original values in your data frame for coloring, not the computed summary statistics. By default grouping will happen on every discrete variable: in your case the x and the value > 0.5. So the stat summary function will compute means for 8 groups, and color the bars based on this. I do not know of any way to color directly based on the computed means. However you could precompute the means in each group, and color based on this pre-computed mean: thus you would have one mean for every x.

Controlling the total width of a barplot

How to get rid of all this space where the blue lines are?
Data:
data = data.frame(is_repeat = c(0,0,0,1,1,1,1,1,1,1),
value = c(12000,8000,20000,14000,15000,11000,20000,60000,20000, 20000))
data$is_repeat = factor(data$is_repeat, levels = c(0,1),
labels = c("One-time", "Repeat"))
Plot:
ggplot(data, aes(is_repeat, value)) +
geom_bar(stat = "identity", width = 0.3) +
ggtitle("Title") +
xlab("Type of event") +
ylab("Total Value") +
ylim(0, 150000) +
theme_minimal()
edit: I looked at that question and it did NOT solve my problem. My guess is that in the other question's plot, there are 4 bars, so it looks filled. I want to reduce the total width of the X axis.
another edit: Added data.
If you are looking to remove the space between the bars completely and you don't mind the width of bars you could do it with:
geom_bar(stat="identity", position="stack", width=1)
or theme(aspect.ratio=1)
And to remove the space from the end of the plot to the bars you need
scale_x_discrete(expand = c(0,0), limits=c("One-time", "Repeat"))
So your code looks like this:
ggplot(data, aes(is_repeat, value)) +
geom_bar(stat="identity", position="stack", width=1) +
ggtitle("Title") +
xlab("Type of event") +
ylab("Total Value") +
ylim(0, 150000) +
scale_x_discrete(expand = c(0,0), limits=c("One-time", "Repeat")) +
theme_minimal()
And the output:
You can add space between bars with changing the width=1

Resources