'Color coding in ggplot using geome_point, while maintaining size of point
I am plotting a dataset in R using three different variables (x=PassingFilerReads, y=meanCoverage
, and size distribution of alignPercentageR1
over those two things), and I am looking to better visualize size the size distribution of align percentage.
right now I am able to see my graph with good layout when I use this code.
ggplot(L2NEW,aes(x=PassingFilterReads,y=meanCoverageP))+
geom_point(aes(size=alignPercentR1)) +
ylim(0,60) +
xlim(0,1500000000) +
geom_hline(yintercept=30,
color = "red",
linetype="dashed")
How can I edit this so that I can have geom_point(aes(size=alignPercentR1))
also include color? Like alignPercentR1>75= "red"
, and Like alignPercentR1<75= "blue"
, and Like alignPercentR1<25= "green"
I have tried a few other ways of adding this to my code, but none have worked how I was hoping.
ggplot(L2NEW,aes(x=PassingFilterReads,y=meanCoverageP))+
geom_point(aes(data=L2NEW$alignPercentR1,
color='cyan',
size=alignpercentR1>75)) +
ylim(0,70) +
xlim(0,1500000000) +
geom_hline(yintercept=30,
color = "red",
linetype="dashed")
Warning: Ignoring unknown aesthetics: data Error in FUN(X[[i]], ...) : object 'alignpercentR1' not found
and
ggplot(L2NEW,aes(x=PassingFilterReads,y=meanCoverageP,))+
geom_point(aes(color="red",
size=alignPercentR1>75) +
ylim(0,70) +
xlim(0,1500000000) +
geom_hline(yintercept=30,
color = "red",
linetype="dashed")
Error: unexpected symbol in: "ggplot(L2NEW,aes(x=PassingFilterReads,y=meanCoverageP,))+geom_point(aes(color="red", size=alignPercentR1>75) + ylim(0,70) + xlim(0,1500000000) +geom_hline(yintercept=30,color = "red", linetype
Solution 1:[1]
It's helpful to provide an example of your data to help answer your question. Have a look here: How to make a great R reproducible example.
To add color, you can do the following:
library(tidyverse)
df <-
tibble(
x = runif(100, 0, 100),
y = runif(100, 0, 100),
z = runif(100, 0, 100)
)
ggplot(df) +
aes(x = x, y = y, size = z, color = z) +
geom_point()
To add your specific colors, the easiest way would be to add the colors into your data frame. This is because you have a continuous variable (size
) to which you want to map a discrete aesthetic (color
).
df <-
mutate(
df,
clr = case_when(
z > 75 ~ "red",
z > 25 ~ "blue",
TRUE ~ "green"
)
)
ggplot(df) +
aes(x = x, y = y, size = z, color = clr) +
geom_point() +
scale_color_identity()
scale_color_identity
will use the literal colors you've added to the clr
column. If you want to define the palette in the plot code, use scale_color_manual
and input your own vector of values.
Solution 2:[2]
What about something like this:
library(dplyr)
L2NEW <- L2NEW %>%
mutate(group = case_when(
alignPercentR1 > 75 ~ "High",
alignPercentR1 <= 75 & alignPercentR1 > 25 ~ "Middle",
alignPercentR1 <= 25 ~ "Low",
TRUE ~ NA_character_
),
group = factor(group, levels=c("Low", "Middle", "High")))
ggplot(L2NEW,aes(x=PassingFilterReads,y=meanCoverageP))+
geom_point(aes(size=alignPercentR1, colour=group)) +
scale_colour_manual(values=c("green", "blue", "red")) +
ylim(0,60) +
xlim(0,1500000000) +
geom_hline(yintercept=30,
color = "red",
linetype="dashed")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Will Oldham |
Solution 2 |