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)
Related
I cannot understand why the legend is not changing given the following code. The same options do the trick with geom_histogram. Thanks in advance for any assistance.
data(mtcars)
ggplot(mtcars, aes(x = disp, color = as.factor(am))) +
geom_density(aes(group = am)) +
theme_classic() +
guides(fill = guide_legend(reverse=TRUE)) +
labs(x = "Displacement", y = "Density") +
scale_fill_manual(name="",values=c("black","gray"),labels=c("Foreign","Domestic"))
You used color in your call of aes(). To modify the scale for this variable you need to use scale_color_manual and not scale_fill_manual.
It's tricky because geom_histogram does use fill, but geom_density uses color.
Working solution :
data(mtcars)
ggplot(mtcars, aes(x = disp, color = as.factor(am))) +
geom_density(aes(group = am)) +
theme_classic() +
guides(fill = guide_legend(reverse=TRUE)) +
labs(x = "Displacement", y = "Density") +
scale_color_manual(name="",values=c("black","gray"),labels=c("Foreign","Domestic"))
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
I tried to set the legend position as a global option but it did not work. I was only able to set a theme as default, but not the legend position, as following:
theme_set(theme_bw()) # Defining the global theme
ggplot(data = iris, aes(x = Petal.Length, y=Sepal.Length, color = Species)) +
geom_point()
I want the legend position to be at the bottom of the plot (as it will work for every plot, i.e. globally). How can I do it?
Cheers
To change the default settings of ggplot2 you can do as follow:
new_theme <- theme_bw() %+replace%
theme(legend.position = "bottom")
theme_set(new_theme)
obs: you can do this for any arg.
It should be sufficient to just add + theme(legend.position = 'bottom') to your theme_set()
library(ggplot2)
theme_set(theme_bw() + theme(legend.position = 'bottom')) # Defining the global theme
ggplot(data = iris, aes(x = Petal.Length, y=Sepal.Length, color = Species)) +
geom_point()
I'll use violin plots here as an example, but the question extends to many other ggplot types.
I know how to subset my data along the x-axis by a factor:
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
geom_violin() +
geom_point(position = "jitter")
And I know how to plot only the full dataset:
ggplot(iris, aes(x = 1, y = Sepal.Length)) +
geom_violin() +
geom_point(position = "jitter")
My question is: is there a way to plot the full data AND a subset-by-factor side-by-side in the same plot? In other words, for the iris data, could I make a violin plot that has both "full data" and "setosa" along the x-axis?
This would enable a comparison of the distribution of a full dataset and a subset of that dataset. If this isn't possible, any recommendations on better way to visualise this would also be welcome :)
Thanks for any ideas!
Using:
ggplot(iris, aes(x = "All", y = Sepal.Length)) +
geom_violin() +
geom_point(aes(color="All"), position = "jitter") +
geom_violin(data=iris, aes(x = Species, y = Sepal.Length)) +
geom_point(data=iris, aes(x = Species, y = Sepal.Length, color = Species),
position = "jitter") +
scale_color_manual(values = c("black","#F8766D","#00BA38","#619CFF")) +
theme_minimal(base_size = 16) +
theme(axis.title.x = element_blank(), legend.title = element_blank())
gives:
I'm trying to customize the look of multiple loess plots within the same graph for different levels of a group var. I looked at this post, but wasn't able to make it work:
ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species, linetype=Species)) +
stat_smooth(method = "loess")
I'd like to change the color of each band and line.
You can specify the looks with for example the scale_color_manual scale. In the example below I also used override.aes within guides to get a nice legend as well:
ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species, linetype=Species)) +
stat_smooth(aes(fill=Species), method = "loess", size=1) +
scale_color_manual(values = c("green","blue","red")) +
scale_fill_manual(values = c("green","blue","red")) +
scale_linetype_manual(values = c("dashed","dotted","solid")) +
theme_bw() +
guides(fill=guide_legend(override.aes = list(fill="white",size=1.2)))
this gives:
Other alternatives to the manual scales are the hue and brewer scales.
I added size = 2 so you can see that the line type is different for each line:
ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, color=Species, linetype=Species)) +
stat_smooth(method = "loess", aes(fill = Species), size= 2)