geom_point not working correctly with ggMarginal - r

Here is a reprex of the issue I'm having:
piris <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) +
geom_point(aes(fill = Species))
ggMarginal(piris, groupFill = TRUE, groupColour = TRUE)
And the resulting plot is as follows:
I would like to change the points within geom_point to be pch = 21 with fill representing the Species (as in the plot above) but color = "black" so that each point has a black border. My problem is that doing so makes my marginal density plots black:
piris <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) +
geom_point(aes(fill = Species), pch = 21, color = "black")
ggMarginal(piris, groupFill = TRUE)
ggMarginal seems to be looking at the border color rather than the aes(color). I removed the groupColor from the second plot, because setting it to TRUE just makes one large black density curve. I have a hunch this has something to do with groupColor and groupFill but I simply cannot figure it out. Any help would be greatly appreciated!!

It fills the marginal density plots with colours according groups, however, their borders are black:
piris <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, colour = Species)) +
geom_point(aes(colour=Species)) +
geom_point(pch = 21, colour = "black")
ggMarginal(piris, groupFill = TRUE)

Related

R ggplot geom_label rotate label

I have a plot with labels. I wanted the bounding background box on the label so I switched from using geom_text to geom_label. When I was using the geom_text option I had my angle = 90. Is there a way to do that with geom_label? Or an alternative method to labeling with a background?
Here is some sample code to play with:
ggplot(data = iris,
aes(x = Sepal.Width, y = Petal.Length)) + geom_point(size = 10)+
geom_label(data = iris,
aes(x = Sepal.Width, y = Petal.Length, label = Species),alpha=0.5)
According to the docs
Currently geom_label() does not support the check_overlap argument or the angle aesthetic.
An alternative would be to use ggtext::geom_richtext:
library(ggplot2)
ggplot(
data = iris,
aes(x = Sepal.Width, y = Petal.Length)
) +
geom_point(size = 10) +
ggtext::geom_richtext(aes(label = Species), alpha = 0.5, angle = 90)

Removing borders around points in ggplot

I have a scatter plot with two different sets of points based on different data sets. I want one of these sets of points to have a border, therefore I have used, pch = 21, which changes the points into circles with a border and an interior.
I want the other set of points to have no border (for example, removing the red borders in the example below). In the example below, I have still added pch = 21 for these points because if I don't use this, then I would have to use colour = Petal.Length, rather than fill = Petal.Length for the colour of the (interior of) the points I do not want to use colour instead of fill because I want the two sets of points to share a legend.
Is there a way to use pch = 21 but then remove the border?
iris2 <- iris %>%
mutate(Sepal.Length = Sepal.Length + 1)
ggplot() +
geom_point(data = iris,
aes(x = Sepal.Length,
y = Sepal.Width,
fill = Petal.Length),
pch = 21, colour = "red", size = 3) +
geom_point(data = iris2,
aes(x = Sepal.Length,
y = Sepal.Width,
fill = Petal.Length),
pch = 21, colour = "black", size = 3)
I have tried using stroke to change the border thickness, but while this makes the borders thicker, stroke = 0 still gives red borders
stroke=NA removes it altogether
ggplot() +
geom_point(data = iris,
aes(x = Sepal.Length,
y = Sepal.Width,
fill = Petal.Length),
pch = 21, colour = "red", size = 3, stroke=NA) +
geom_point(data = iris2,
aes(x = Sepal.Length,
y = Sepal.Width,
fill = Petal.Length),
pch = 21, colour = "black", size = 3)
You just need to put colour = Petal.Length inside aes() and remove the colour attribute outside.
Like this:
ggplot() +
geom_point(data = iris,
aes(x = Sepal.Length,
y = Sepal.Width,
fill = Petal.Length,
colour = Petal.Length),
pch = 21, size = 3) +
geom_point(data = iris2,
aes(x = Sepal.Length, y = Sepal.Width, fill = Petal.Length),
pch = 21, colour = "black", size = 3)

Make stat_ellipse {ggplot2} outline geom_point fill color

I have a scatterplot with 15 groupings. I am using geom_point() with shape = 21 so that I can have fill and color (outline color). I am using black for the outline color to give better contrast between the similar colors in my legend. When I add a stat_ellipse() though, it makes the ellipse outline black.
I want this, with black outlines around the points:
groupings <- paste0("Group", 1:15)
iris$group <- rep(groupings, 10)
iris_plot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point(aes(Sepal.Length, Sepal.Width, colour = factor(iris$group))) + stat_ellipse(data = iris, aes(color = factor(iris$group)))
iris_plot
But when I add the black outlines around the points, it turns my ellipses black, making them impossible to interpret.
library(RColorBrewer)
groupings <- paste0("Group", 1:15)
iris$group <- rep(groupings, 10)
fill_colors <- scales::hue_pal()(15)
outline_colors <- rep("black", 15)
iris_plot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point(aes(Sepal.Length, Sepal.Width, colour = factor(iris$group), fill = factor(iris$group)), shape = 21) + stat_ellipse(data = iris, aes(color = factor(iris$group))) + scale_colour_manual(name = "Grouping", labels = sort(unique(factor(iris$group))), values = outline_colors) + scale_fill_manual(name = "Grouping", labels = sort(unique(factor(iris$group))), values = fill_colors)
iris_plot
I do not want a fill color because there is so much overlap between ellipses that it becomes impossible to see anything.
Thank you for your time.
I think you need to pass color outside aes for geom_point, otherwise when you are applying scale_color_manual, it will apply both for geom_point and stat_ellipse:
iris_plot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point(aes(Sepal.Length, Sepal.Width,fill = group), color = "black", shape = 21) +
stat_ellipse(data = iris, aes(color = group)) +
scale_fill_manual(name = "Grouping", labels = sort(unique(factor(iris$group))), values = fill_colors)+
scale_color_manual(name = "Grouping", values = fill_colors, labels = sort(unique(factor(iris$group))))
iris_plot

How to provide multiple fill values using the same aesthetic

My goal is to pass separate values to change the colors used for fill aesthetics in different geoms.
For example:
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
stat_summary(aes(fill = Species), color = 'black', geom = 'bar', fun.y = mean) +
geom_point(aes(fill = Species), color = 'black', shape = 21) +
scale_fill_manual(values = c('royal blue', 'red2', 'limegreen'))
In this plot, I would like to be able to use separate colors to fill the bars and points. Is this possible? I'm aware of using scale_fill_manual()
to set the colors to whatever values I want, but this will change the fills of both the bars and the points to the same colors.
Here is a semi-working example of what I am trying to do, however, the legend is off...
iris_j <- iris %>%
mutate(Species_bar = factor(paste0(Species, '_bar')))
color.groups <- c('royal blue', 'red2', 'limegreen', NA, 'royal blue', 'white')
names(color.groups) <- c(levels(iris_j$Species), levels(iris_j$Species_bar))
ggplot(iris_j, aes(x = Species, y = Sepal.Length)) +
stat_summary(aes(fill = Species_bar), color = 'black', geom = 'bar', fun.y = mean) +
geom_point(aes(fill = Species), color = 'black', shape = 21) +
scale_fill_manual(values = color.groups)
This is one of the limitations of ggplot—an aesthetic can only be mapped to a single variable. Generally speaking, I find it a reasonable limitation, as it forestalls a lot of confusing and hard-to-read graphs. That said, with some creativity, it can be worked around, e.g. by coloring the points with the color aesthetic, and then overplotting to add a stroke:
library(ggplot2)
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
stat_summary(aes(fill = Species), color = 'black', geom = 'bar', fun.y = mean) +
geom_point(aes(color = Species)) + # add colored points
geom_point(color = 'black', shape = 21, show.legend = TRUE) + # add point strokes (including in legend)
scale_color_manual(values = c('royal blue', 'red2', 'limegreen')) + # define point colors
scale_fill_manual(values = c(NA, 'royal blue', 'white')) # define bar colors
To separate the legends, specify a different name for each. To add a stroke to the points in the legend, you'll need to effectively rebuild it in guide_legend. (According to the docs, supplying a named vector to show.legend should work, but in practice it fails.)
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
stat_summary(aes(fill = Species), color = 'black', geom = 'bar', fun.y = mean) +
geom_point(aes(color = Species)) +
geom_point(color = 'black', shape = 21) +
scale_color_manual('points', values = c('royal blue', 'red2', 'limegreen'),
guide = guide_legend(override.aes = list(shape = 21, color = 'black',
fill = c('royal blue', 'red2', 'limegreen')))) +
scale_fill_manual('bars', values = c(NA, 'royal blue', 'white'))
Such an approach will not generalize to a plot where color is already being used otherwise.
Here are a few little things to try that you could build off of.
First up, if you don't need to use a filled shape, you can just map color to the species in geom_point, so you have a color scale and a fill scale. In this case, I changed the label for fill to mark it as being the means, to show how you can split these into two legends.
library(tidyverse)
light_colors <- c("#87CEEB", "#FFB6C1", "#FF8C69")
dark_colors <- c("#22A0D6", "#E33650", "#BF411B")
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
stat_summary(aes(fill = Species), geom = "bar", fun.y = mean) +
geom_point(aes(color = Species)) +
scale_fill_manual(values = light_colors) +
scale_color_manual(values = dark_colors) +
labs(fill = "Mean by Species")
Second, if you do need a filled shape, let geom_point get a fill scale and hack the bars to have a color instead. One way to do that is by making what look like bars but are actually really big geom_segments. I changed the size in the legend to make the legend keys not ridiculously huge.
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
stat_summary(aes(xend = Species, yend = 0, color = Species), geom = "segment", fun.y = mean, size = 30, lineend = "butt") +
geom_point(aes(fill = Species), color = "black", shape = 21) +
scale_fill_manual(values = light_colors) +
scale_color_manual(values = dark_colors, guide = guide_legend(override.aes = list(size = 4)))
Third way, make a data frame of averages and give it a variable to denote that it's got averages, then add a variable to the original data frame to denote that it's observations. Then you can map the interaction of type with species to get separate colors in one fill scale.
avgs <- iris %>%
group_by(Species) %>%
summarise(Sepal.Length = mean(Sepal.Length)) %>%
mutate(type = "Mean")
iris %>%
select(Species, Sepal.Length) %>%
mutate(type = "Observation") %>%
ggplot(aes(x = Species, y = Sepal.Length, fill = interaction(Species, type))) +
geom_col(data = avgs) +
geom_point(color = "black", shape = 21)
Not quite a perfect solution, but it may be a sufficient workaround
cols_1 <- c("red", "green", "blue")
cols_2 <- c("orange", "purple", "yellow")
ggplot(iris, aes(x = Species, y = Sepal.Length)) +
geom_point(aes(color = Species)) + # Using color instead of fill
stat_summary(aes(fill = Species), color = 'black', geom = 'bar', fun.y = mean, alpha = c(0.5, 0.05, 1)) +
scale_color_manual(values = cols_1) + # colors your points
scale_fill_manual(values = cols_2) # fills your Summary Bars
Adjust the colors, alpha, and other graphical parameters as you see fit.

All my plots in Rstudio just show up as gray boxes?

I am using ggplot and geoms to show my data, but the plot sidebar area just shows a gray box with the x and y axis correctly labeled.
Here is the output image:
The code which made the plot:
ggplot(Wc, aes(y = popsafe, x = rnground)) +
geom_jitter(aes(col = me)) +
geom_smooth(method = "lm", se = FALSE, col = "black")
Looks like your dataset is empty. We don't know what your dataset contains, so here an example with the built-in iris dataset. First a proper plot, using the same geoms and mappings you use:
library(ggplot2)
ggplot(iris, aes(y = Sepal.Length, x = Sepal.Width)) +
geom_jitter(aes(col = Species)) +
geom_smooth(method = "lm", se = FALSE, col = "black")
Now I remove all the data from the dataset and replot:
library(dplyr)
iris_empty <- filter(iris, Sepal.Length < 0)
ggplot(iris_empty, aes(y = Sepal.Length, x = Sepal.Width)) +
geom_jitter(aes(col = Species)) +
geom_smooth(method = "lm", se = FALSE, col = "black")
A simple head(Wc) would confirm whether your dataset actually contains any data.

Resources