How to add a legend to multiple plots in R [duplicate] - r

This question already has an answer here:
regrading adding a legend using ggplot2 for different lines
(1 answer)
Closed 2 years ago.
I have this code:
testPlot= ggplot(residFrame) +
geom_point(aes(x=STATEFP, y=total_diff, colour='total'), colour='red', shape=1) +
geom_point(aes(x=STATEFP, y=desalination_diff, colour='desalination'), colour='blue', shape=1) +
geom_point(aes(x=STATEFP, y=surfacewater_diff), colour='green', shape=1) +
geom_point(aes(x=STATEFP, y=groundwater_diff), colour='yellow', shape=1) +
xlab('STATEFP') + ylab('Difference') + ggtitle('Difference for all states', subtitle='For each source')
testPlot
And now I want to add a legend to testPlot that describes what the colours in the plot represent. I have searched the web, but cannot find the answer to this particular problem, can someone help me out here?
Thanks!

You should get the data in long format and then plot instead of calling geom_point multiple times. You have not provided an example of your data but you can try.
library(ggplot2)
residFrame %>%
tidyr::pivot_longer(cols = ends_with('diff')) %>%
ggplot() + aes(STATEFP, value, color = name) +
geom_point(shape = 1) +
xlab('STATEFP') + ylab('Difference') +
ggtitle('Difference for all states', subtitle='For each source')

Related

ggplot2 geom_line colors by group but with different colorcodes [duplicate]

This question already has answers here:
How to set multiple legends / scales for the same aesthetic in ggplot2?
(2 answers)
Manually setting group colors for ggplot2
(1 answer)
Changing line colors with ggplot()
(2 answers)
Closed 10 months ago.
does anyone know how I can plot several line graphs in one graph with different color codes?
ggplot(df, aes(x=variable1)) +
geom_line(aes(y=variable2,color=group1))+
geom_line(aes(y=variable3,color=group1))
I would like to have one color code for the first geom_line and a different one for the second geom_line.
color_group <- c("blue","black","yellow2","orange")
color_flag <- c("green","red","yellow2","cyan")
With
scale_colour_manual(values=color_group)
I can only assign a color code to both of them simultaneously and not separately. Thanks for your help!
You could use the ggnewscale package
library(ggnewscale)
ggplot(df, aes(x = variable1)) +
geom_line(aes(y = variable2, color = group1)) +
scale_colour_manual(values = color_group) +
new_scale_color() +
geom_line(aes(y = variable3, color = group1)) +
scale_colour_manual(values = color_flag)

Creating a ggplot boxplot with jitter duplicates my data (R) [duplicate]

This question already has answers here:
R ggplot geom_jitter duplicates outlier
(1 answer)
How to exclude outliers when using geom_boxplot() + geom_jitter() in R
(2 answers)
Closed last year.
I want to create a boxplot that shows individual data points as well.
This is the code that I am using:
ggplot(data, aes(x=treatment, y=aggregate_count, color = treatment)) +
geom_boxplot() +
geom_point(position = "jitter") +
ylab("Aggregate Count") +
xlab("") +
theme_classic()
Using both the geom_boxplot() and geom_point() function together like this however duplicates my dataset. I noticed this because there is only one value in my dataset with a value above 30, but in the plot, I can see two. If I remove either geom_boxplot() or geom_point() the data gets displayed correctly.
Does someone have an idea on how to fix this?
Thank you in advance!!

How to Add Lines With A Facet R [duplicate]

This question already has answers here:
facet_wrap add geom_hline
(2 answers)
Closed 5 months ago.
So I have a faceted graph, and I want to be able to add lines to it that change by each facet.
Here's the code:
p <- ggplot(mtcars, aes(x=wt))+
geom_histogram(bins = 20,aes(fill = factor(cyl)))+
facet_grid(.~cyl)+
scale_color_manual(values = c('red','green','blue'))+
geom_vline(xintercept = mean(mtcars$wt))
p
So my question is, how would I get it so that the graph is showing the mean of each faceted sub-graph.
I hope that makes sense and appreciate your time regardless of your answering capability.
You can do this within the ggplot call by using stat_summaryh from the ggstance package. In the code below, I've also changed scale_colour_manual to scale_fill_manual on the assumption that you were trying to set the fill colors of the histogram bars:
library(tidyverse)
library(ggstance)
ggplot(mtcars, aes(x=wt))+
geom_histogram(bins = 20,aes(fill = factor(cyl)))+
stat_summaryh(fun.x=mean, geom="vline", aes(xintercept=..x.., y=0),
colour="grey40") +
facet_grid(.~cyl)+
scale_fill_manual(values = c('red','green','blue')) +
theme_bw()
Another option is to calculate the desired means within geom_vline (this is an implementation of the summary approach that #Ben suggested). In the code below, the . is a "pronoun" that refers to the data frame (mtcars in this case) that was fed into ggplot:
ggplot(mtcars, aes(x=wt))+
geom_histogram(bins = 20,aes(fill = factor(cyl)))+
geom_vline(data = . %>% group_by(cyl) %>% summarise(wt=mean(wt)),
aes(xintercept=wt), colour="grey40") +
facet_grid(.~cyl)+
scale_fill_manual(values = c('red','green','blue')) +
theme_bw()

Different `geom_hline()` for each facet of ggplot [duplicate]

This question already has an answer here:
Display a summary line per facet rather than overall
(1 answer)
Closed 4 years ago.
library(tidyverse)
ggplot(mpg, aes(cty, hwy)) +
geom_point() +
facet_grid(year ~ fl) +
geom_hline(yintercept = mean(mpg$hwy))
I want each geom_hline() in the facet shown above to be the mean of the points that are only contained within that facet. I would think that I could do it with something like (below). But that doesn't work. I'm close, right?
library(tidyverse)
ggplot(mpg, aes(cty, hwy)) +
geom_point() +
facet_grid(year ~ fl) +
geom_hline(yintercept = mean(mpg %>% group_by(year, fl)$hwy))
If you have the value you wish to use for each facet as a column in the data frame, and that value is unique within each facet, then you can use geom_hline(aes(yintercept=column)), which will then plot a horizontal line for each of the facets

ggplot transparency on individual bar

I am currently attempting to use ggplot to create a bar chart with a single bar that is partially transparent.
I have the following code:
dt1 <- data.table(yr=c(2010,2010,2011,2011),
val=c(1500,3000,2000,1100),
x=c("a","b","a","b"))
ggplot() + geom_bar(data=dt1, aes(x=yr, y=val,fill=x),stat="identity") +
scale_x_continuous(breaks=dt1$yr)
This will create a simple chart with 2 columns with stacked data. I have tried the following code to adjust the 2011 value to have transparency, however I am not having much luck. Any pointers?
dt1[,alphayr:=ifelse(yr==2011,.5,1)]
ggplot() + geom_bar(data=dt1, aes(x=yr, y=val,fill=x),stat="identity", alpha=dt1$alphayr) +
scale_x_continuous(breaks=dt1$yr)
First you put the alpha inside the aes as suggested by #jazzurro. However, you should use factor for this to get a discrete scale. Then you can manually adjust the alpha scale.
ggplot() + geom_bar(data=dt1, aes(x=yr, y=val, fill=x, alpha=factor(alphayr)), stat="identity") +
scale_x_continuous(breaks=dt1$yr) +
scale_alpha_manual(values = c("0.5"=0.5, "1"=1), guide='none')
An instructive question and answer. Other readers may not use data.table syntax and may want to see the result, so I simply revised #shadow's answer to create a factor with a data frame, and display the plot below.
dt1 <- data.frame(yr=c(2010,2010,2011,2011), val=c(1500,3000,2000,1100), x=c("a","b","a","b"))
create the factor
dt1$alphayr <- as.factor(ifelse(dt1$yr == "2011", 0.5, 1))
ggplot() + geom_bar(data=dt1, aes(x=yr, y=val, fill=x, alpha=factor(alphayr)), stat="identity") +
scale_x_continuous(breaks=dt1$yr) +
scale_alpha_manual(values = c("0.5"=0.5, "1"=1), guide='none')

Resources