Context: I am trying to convert facet_grid to facet_wrap to add ncols. I know the cols=conference will change, but I am not sure how.
graph <- ggplot(data, aes(week, positivity_rate, color=school)) +
geom_line() +
facet_grid(cols=vars(conference))
graph
library(tidyverse)
ggplot(iris, aes(Petal.Length, Petal.Width, color=Species)) +
geom_line() +
facet_grid(cols=vars(Species))
ggplot(iris, aes(Petal.Length, Petal.Width, color=Species)) +
geom_line() +
facet_wrap(~Species,ncol = 2)
Related
Is there a way to mirror the x-/y-axis to the opposite side (only outside, no axis on the inside)? My goal is to get something like:
MWE
library(ggplot2)
ggplot(data = iris, aes(x=Sepal.Length, y=Sepal.Width)) +
geom_point() +
facet_grid(rows = "Species")
This should work:
ggplot(data = iris, aes(x=Sepal.Length, y=Sepal.Width)) +
geom_point() +
scale_y_continuous(sec.axis = sec_axis(~.*1))+
scale_x_continuous(sec.axis = sec_axis(~.*1))+
facet_grid(rows = "Species")
Yes, you can use the guides() function to set axes for secondary positions as well, see example below. To have it more like your example, the strip placement should be changed to "outside".
library(ggplot2)
ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point() +
facet_grid(rows = vars(Species)) +
guides(
x.sec = "axis", y.sec = "axis"
) +
theme(strip.placement = "outside")
Created on 2022-06-07 by the reprex package (v2.0.0)
I recently started learning R but am confused with the aes feature in ggplot2.
I have seen two different places where aes is placed in the code.
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point()
What is the difference between the two?
Can't find a dupe, so here's an answer:
Aesthetics specified in ggplot() are inherited by subsequent layers. Aesthetics specified in particular layers are specific only to that layer. Here's an example:
library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
geom_smooth()
ggplot(mtcars) +
geom_point(aes(wt, mpg)) +
geom_smooth() # error, geom_smooth needs its own aesthetics
This is mostly useful when you want different layers to have different specifications, for example these two plots are different, you have to decide which you want:
ggplot(mtcars, aes(wt, mpg, color = factor(cyl))) +
geom_point() +
geom_smooth()
ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(color = factor(cyl))) +
geom_smooth()
On individual layers, you can use inherit.aes = FALSE to turn off inheritance for that layer. This is very useful if most of your layers use the same aesthetics, but a few do not.
I have a column with color codes but when I use them I dont see any legend.
Example:
library(ggplot2)
iris$col = rep(c('#b580d1', '#9bb240', '#6a70d7'), each = 50)
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = iris$col)) +
geom_line() +
geom_point() +
scale_color_identity()
I would like to have the same legend, but with the different colors as in:
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
geom_line() +
geom_point()
library(ggplot2)
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
geom_line() +
geom_point() +
scale_color_manual(values= c('#b580d1', '#9bb240', '#6a70d7'))
I'm trying to simplify my plots in ggplot2. Suppose I want to create a scatterplot from iris dataset:
ggplot(iris, aes(x=Petal.Length, y=Petal.Width, colour=Species)) +
geom_point()
But suppose I don't like ggplot2 default theme and palette. Let's say I want to use theme_bw and Dark2 palette:
ggplot(iris, aes(x=Petal.Length, y=Petal.Width, colour=Species)) +
geom_point() +
theme_bw() +
scale_color_brewer(palette="Dark2")
And suppose I have lots of plots and I want all of them using theme_bw and Dark2 palette. I know I can use theme_set(theme_bw()) to make all my plots have the black and white theme. Is there a similar function to make all my plots use Dark2 palette? In other words, how can I run a code like
ggplot(iris, aes(x=Petal.Length, y=Petal.Width, colour=Species)) +
geom_point()
and have theme_bw and Dark2 palette in all my plots?
One solution would be to write a custom wrapper:
ggcust <- function(...){
ggplot(...) +
theme_bw()
}
fill in all the theme options you need, then use it like this:
ggcust(data = mtcars, aes(x = mpg, y = cyl)) +
geom_point()
You can also put layers into a list:
gglayer_theme <- list(
theme_bw(),
scale_color_brewer(palette="Dark2")
)
And treat the list like a new layer (Note + becomes , in this list notation):
ggplot(iris, aes(x=Petal.Length, y=Petal.Width, colour=Species)) +
geom_point() +
gglayer_theme
The advantage to the custom wrapper approach is the ability to easily mix layers:
gglayer_labs <- list(
labs(
x = "x",
y = "y"
)
)
ggplot(iris, aes(x=Petal.Length, y=Petal.Width, colour=Species)) +
geom_point() +
gglayer_theme +
gglayer_labs
Or combine them beforehand:
gglayer_all <- c(gglayer_theme, gglayer_labs)
ggplot(iris, aes(x=Petal.Length, y=Petal.Width, colour=Species)) +
geom_point() +
gglayer_all
first, sorry for my English and mistakes.
I have a plot like this:
data <- data.frame(site=rep(letters[1:6],each=3), year=rep(2001:2003, 6), nb=round(runif(18, min=20, max=60)), group=c(rep("A",9),rep("B", 6),rep("C",3)))
ggplot(data=data, aes(x= factor(year), y= nb)) +
geom_point() +
facet_wrap(~site)
And I would like to add the other panel "group". In fact I would like to make this graph without empty parts:
ggplot(data=data, aes(x= factor(year), y= nb)) +
geom_point() +
facet_grid(group~site)
Does someone has an idea? Thanks for you help!
#
There is this solution which look like that I want, but I thought there were more simple solution :
plt1 <- ggplot(data=data[data$group=="A",], aes(x= factor(year), y= nb)) +
geom_point() +
ggtitle("A")+
facet_grid(~site)+
xlab("") + ylab("")
plt2 <- ggplot(data=data[data$group=="B",], aes(x= factor(year), y= nb)) +
geom_point() +
ggtitle("B")+
facet_grid(~site)+
xlab("") + ylab("")
plt3 <- ggplot(data=data[data$group=="C",], aes(x= factor(year), y= nb)) +
geom_point() +
ggtitle("C")+
facet_grid(~site)+
xlab("") + ylab("")
library(gridExtra)
grid.arrange(arrangeGrob(plt1,plt2, plt3),
left = textGrob("nb",rot=90))
You can combine the site and group inside the facet_wrap() - so you will have only "full" facets.
ggplot(data=data, aes(x= factor(year), y= nb)) +
geom_point() +
facet_wrap(~site+group)