mix discrete and continuous values to get a fill guide in ggplot2 - r

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)))

Related

fill and scale_color in ggplot

I am trying to color bars in ggplot but having issues. Can someone explain how to correctly use the fill parameter and the scale_colour parameters?
library(ggplot2)
df<-data.frame(c(80,33,30),c("Too militarized","Just doing their job","Unfairly tarnished by a few"),c("57%","23%","21%"))
colnames(df)<-c("values","names","percentages")
ggplot(df,aes(names,values))+
geom_bar(stat = "identity",position = "dodge",fill=names)+
geom_text(aes(label=percentages), vjust=0)+
ylab("percentage")+
xlab("thought")+
scale_colour_manual(values = rainbow(nrow(df)))
Working barplot example
barplot(c(df$values),names=c("Too militarized","Just doing their job","Unfairly tarnished by a few"),col = rainbow(nrow(df)))
The main issue is that you don't have fill inside a call to aes in geom_bar(). When mapping from data to visuals like colors, it has to be inside aes(). You can fix this by either wrapping fill=names with aes() or by just specifying fill colors directly, instead of using names:
Option 1 (no legend):
ggplot(df, aes(names, values)) +
geom_bar(stat="identity", fill=rainbow(nrow(df))) +
ylab("percentage") +
xlab("thought")
Option 2 (legend, because mapping from data to colors):
ggplot(df, aes(names, values)) +
geom_bar(stat="identity", aes(fill=names)) +
ylab("percentage") +
xlab("thought") +
scale_fill_manual(values=rainbow(nrow(df)))
Note that in both cases you might want to explicitly factor df$names ahead of the call to ggplot in order to get the bars in the order you want.

How can I use different color or linetype aesthetics in same plot with ggplot?

I'm creating a plot with ggplot that uses colored points, vertical lines, and horizontal lines to display the data. Ideally, I'd like to use two different color or linetype scales for the geom_vline and geom_hline layers, but ggplot discourages/disallows multiple variables mapped to the same aesthetic.
# Create example data
library(tidyverse)
library(lubridate)
set.seed(1234)
example.df <- data_frame(dt = seq(ymd("2016-01-01"), ymd("2016-12-31"), by="1 day"),
value = rnorm(366),
grp = sample(LETTERS[1:3], 366, replace=TRUE))
date.lines <- data_frame(dt = ymd(c("2016-04-01", "2016-10-31")),
dt.label = c("April Fools'", "Halloween"))
value.lines <- data_frame(value = c(-1, 1),
value.label = c("Threshold 1", "Threshold 2"))
If I set linetype aesthetics for both geom_*lines, they get put in the
linetype legend together, which doesn't necessarily make logical sense
ggplot(example.df, aes(x=dt, y=value, colour=grp)) +
geom_hline(data=value.lines, aes(yintercept=value, linetype=value.label)) +
geom_vline(data=date.lines, aes(xintercept=as.numeric(dt), linetype=dt.label)) +
geom_point(size=1) +
scale_x_date() +
theme_minimal()
Alternatively, I could set one of the lines to use a colour aesthetic,
but then that again puts the legend lines in an illogical legend
grouping
ggplot(example.df, aes(x=dt, y=value, colour=grp)) +
geom_hline(data=value.lines, aes(yintercept=value, colour=value.label)) +
geom_vline(data=date.lines, aes(xintercept=as.numeric(dt), linetype=dt.label)) +
geom_point(size=1) +
scale_x_date() +
theme_minimal()
The only partial solution I've found is to use a fill aesthetic instead
of colour in geom_pointand setting shape=21 to use a fillable shape,
but that forces a black border around the points. I can get rid of the
border by manually setting color="white, but then the white border
covers up points. If I set colour=NA, no points are plotted.
ggplot(example.df, aes(x=dt, y=value, fill=grp)) +
geom_hline(data=value.lines, aes(yintercept=value, colour=value.label)) +
geom_vline(data=date.lines, aes(xintercept=as.numeric(dt), linetype=dt.label)) +
geom_point(shape=21, size=2, colour="white") +
scale_x_date() +
theme_minimal()
This might be a case where ggplot's "you can't have two variables mapped
to the same aesthetic" rule can/should be broken, but I can't figure out clean way around it. Using fill with geom_point shows the most promise, but there's no way to remove the point borders.
Any ideas for plotting two different color or linetype aesthetics here?

How to change style settings in stacked barchart overlaid with density line (ggplot2)

I am trying to change the style settings of this kind of chart and hope you can help me.
R code:
set_theme(theme_bw)
cglac$pred2<-as.factor(cglac$pred)
ggplot(cglac, aes(x=depth, colour=pred2))
+ geom_bar(aes(y=..density..),binwidth=3, alpha=.5, position="stack")
+ geom_density(alpha=.2)
+ xlab("Depth (m)")
+ ylab("Counts & Density")
+ coord_flip()
+ scale_x_reverse()
+ theme_bw()
which produces this graph:
Here some points:
What I want is to have the density line as black and white lines separated by symbols rather than colour (dashed line, dotted line etc).
The other thing is the histogram itself. How do I get rid of the grey background in the bars?
Can I change the bars also to black and white symbol lines (shaded etc)? So that they would match the density lines?
Last but not least I want to add a second x or in this case y axis, because of flip_coord(). The one I see right now is for the density. The other one I need would then be the count data from the pred2 variable.
Thanks for helping.
Best,
Moritz
Have different line types: inside aes(), put linetype = pred2. To make the line color black, inside geom_density, add an argument color = "black".
The "background" of the bars is called "fill". Inside geom_bar, you can set fill = NA for no fill. A more common approach is to fill in the bars with the colors, inside aes() specify fill = pred2. You might consider faceting by your variable, + facet_wrap(~ pred2, nrow = 1) might look very nice.
Shaded bars in ggplot? No, you can't do that easily. See the answers to this question for other options and hacks.
Second y-axis, similar to the shaded symbol lines, the ggplot creator thinks a second y-axis is a terrible design choice, so you can't do it at all easily. Here's a related question, including Hadley's point of view:
I believe plots with separate y scales (not y-scales that are transformations of each other) are fundamentally flawed.
It's definitely worth considering his point of view, and asking yourself if those design choices are really what you want.
Different linetypes for densities
Here's my built-in data version of what you're trying to do:
ggplot(mtcars, aes(x = hp,
linetype = cyl,
group = cyl,
color = cyl)) +
geom_histogram(aes(y=..density.., fill = cyl),
alpha=.5, position="stack") +
geom_density(color = "black") +
coord_flip() +
theme_bw()
And what I think you should do instead. This version uses facets instead of stacking/colors/linetypes. You seem to be aiming for black and white, which isn't a problem at all in this version.
ggplot(mtcars, aes(x = hp,
group = cyl)) +
geom_histogram(aes(y=..density..),
alpha=.5) +
geom_density() +
facet_wrap(~ cyl, nrow = 1) +
coord_flip() +
theme_bw()

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.

Plot thick line with dark dots at data points in ggplot2

I want to plot a path and show where the datapoints are.
Combine Points with lines with ggplot2
uses geom_point() + geom_line() but I do not like that the dots are much thicker and the lines have a discontinuous look - x - x ----- x --- thus I decidet to
create my own dotted line:
mya <- data.frame(a=1:20)
ggplot() +
geom_path(data=mya, aes(x=a, y=a, colour=2, size=1)) +
geom_point(data=mya, aes(x=a, y=a, colour=1, size=1)) +
theme_bw() +
theme(text=element_text(size=11))
I like that the dots and the line have the same size. I did not use the alpha channel because I fear trouble with the alpha channel when I include the files in other programs.
open problems:
R should not create those legends
can R calculate the "darker colour" itself? darker(FF0000) = AA0000
how can I manipulate the linethickness? The size= parameter did not work as expected in R 2.15
Aesthetics can be set or mapped within a ggplot call.
An aesthetic defined within aes(...) is mapped from the data, and a legend created.
An aesthetic may also be set to a single value, by defining it outside aes().
In your case it appears you want to set the size to a single value. You can also use scale_..._manual(values = ..., guide = 'none') to suppress the creation of a legend.
This appears to be what you want with colour.
You can then use named colours such as lightblue and darkblue (see ?colors for more details)
ggplot() +
geom_line(data=mya, aes(x=a, y=a, colour='light'), size = 2) +
geom_point(data=mya, aes(x=a, y=a, colour='dark'), size = 2) +
scale_colour_manual(values = setNames(c('darkblue','lightblue'),
c('dark','light')), guide = 'none') +
theme_bw()

Resources