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

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

Related

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 a legend to multiple plots in R [duplicate]

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

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

How to avoid overlapping in ggplot2 boxplot? [duplicate]

This question already has an answer here:
Spacing between boxplots in ggplot2
(1 answer)
Closed 6 years ago.
I want to plot the boxplot:
library("ggplot2")
p <- ggplot(mpg, aes(class, hwy))
p + geom_boxplot(aes(colour = drv))
but the boxplot showed overlapped within each class.
How can I add distance between boxes?
just try following variant
library("ggplot2")
dodge <- position_dodge(width = 0.9)
p <- ggplot(mpg, aes(class, hwy))
p+geom_boxplot(aes(fill = drv),position=dodge)
With position=dodge you can set the distance between boxplots
Hope this helps
Best
Pavlo

ggplot2: how to show the legend [duplicate]

This question already has answers here:
Add legend to ggplot2 line plot
(4 answers)
Closed 2 years ago.
I made a simple classic plot with ggplot2 which is two graphs in one. However, I'm struggling in showing the legend. It's not showing the legend. I didn't use the melt and reshape way, I just use the classic way. Below is my code.
df <- read.csv("testDataFrame.csv")
graph <- ggplot(df, aes(A)) +
geom_line(aes(y=res1), colour="1") +
geom_point(aes(y=res1), size=5, shape=12) +
geom_line(aes(y=res2), colour="2") +
geom_point(aes(y=res2), size=5, shape=20) +
scale_colour_manual(values=c("red", "green")) +
scale_x_discrete(name="X axis") +
scale_y_continuous(name="Y-axis") +
ggtitle("Test")
#scale_shape_discrete(name ="results",labels=c("Res1", "Res2"),solid=TRUE)
print(graph)
the data frame is:
A,res1,res2
1,11,25
2,29,40
3,40,42
4,50,51
5,66,61
6,75,69
7,85,75
Any suggestion on how to show the legend for the above graph?
In ggplot2, legends are shown for every aesthetic (aes) you set; such as group, colour, shape. And to do that, you'll have to get your data in the form:
A variable value
1 res1 11
... ... ...
6 res1 85
7 res2 75
You can accomplish this with reshape2 using melt (as shown below):
require(reshape2)
require(ggplot2)
ggplot(dat = melt(df, id.var="A"), aes(x=A, y=value)) +
geom_line(aes(colour=variable, group=variable)) +
geom_point(aes(colour=variable, shape=variable, group=variable), size=4)
For example, if you don't want colour for points, then just remove colour=variable from geom_point(aes(.)). For more legend options, follow this link.

Resources