How to add a condition to the geom_point size?
scale_size_manual sets the sizes for a discrete variable.
geom_point(aes(size =n_in_stat>4)) + scale_size_manual(values=c(2,5))
ggplot, conditional fill geom_point
an alternative approach can be the usage of to specific shapes and assign them manually. A nice side effect is a more descriptive legend:
mydata <- tibble(x_var = 1:5,
y_var = runif(5),
category_a = c('yes', 'yes', 'no', 'no', 'yes'),
category_b = c('down', 'up', 'up', 'down', 'down'))
# choose circle and dot shapes
shps <- c(1, 16)
ggplot(mydata, aes(x = x_var, y = y_var, color = category_a, shape= category_b)) +
geom_point(size = 5, stroke = 2) +
# add manually choosen shapes and colors
scale_shape_manual(values=shapes)
Setting absolute point size for geom_point with scale_size_area
You can play around with the range
and limits
arguments within scale_size
to get something closer to what you're looking for:
ggplot(data.frame(area = seq(from = 0, to = 1, length.out = 17), y = 1), aes(x = area, y = y)) +
geom_point(aes(size = area), color = "red") + # Area point
geom_point() + # Default point
scale_size("size_area", range = c(-20, 10))
EDIT:
Since that's a little hacky and not scalable, the better way to do this is to first figure out what the default point size is:
default_size <- ggplot2:::check_subclass("point", "Geom")$default_aes$size
default_size
[1] 1.5
It should be 1.5, unless you've manually changed the defaults. Now we can rebuild the plot and figure out how the size aesthetic is currently being mapped to area:
df <- data.frame(area = seq(from = 0, to = 1, length.out = 17), y = 1)
g <- ggplot(df, aes(x = area, y = y)) +
geom_point(aes(size = area), color = "red") + # Area point
geom_point() +
scale_size_area()
g2 <- ggplot_build(g)
g2$data[[1]] %>%
select(x, size)
x size
1 0.0000 0.000000
2 0.0625 1.500000
3 0.1250 2.121320
4 0.1875 2.598076
5 0.2500 3.000000
6 0.3125 3.354102
7 0.3750 3.674235
8 0.4375 3.968627
9 0.5000 4.242641
10 0.5625 4.500000
11 0.6250 4.743416
12 0.6875 4.974937
13 0.7500 5.196152
14 0.8125 5.408327
15 0.8750 5.612486
16 0.9375 5.809475
17 1.0000 6.000000
The relationship is size = 6*sqrt(x). Why 6? Because the scale_size_area has a default max_size
of 6. So, to make it so the x-value of 0.5 maps to 1.5 size, we have to solve the above equation for a new max_size
, and we get 1.5/sqrt(0.5).
To automate this, we can do the following:
default_size_val <- 0.5
max_size <- default_size/(sqrt(default_size_val))
ggplot(df, aes(x = area, y = y)) +
geom_point(aes(size = area), color = "red") + # Area point
geom_point() +
scale_size_area(max_size = max_size)
Adjust point size to individual variables using geom_point in R
Decided to make my comment an answer. You need to group by variable before scaling.
library(tidyverse)
data <- tibble::tibble(
value = c(4.07, 5.76, 2.87,4.94, 5.48, 6.75,1.53, 1.35, 1.32),
Variable = rep(c(rep("A",3),rep("B",3), rep("C",3))),
Experiment = rep(c(1:3),3))
data <- data %>%group_by(Variable)%>%
mutate(scaled_val = scale(value)) %>%
ungroup()
data$Variable <- factor(data$Variable,levels=rev(unique(data$Variable)))
ggplot(data, aes(x = Experiment, y = Variable, label=NA)) +
geom_point(aes(size = scaled_val, colour = value)) +
geom_text(hjust = 1, size = 2) +
# scale_size(range = c(1,3)) +
theme_bw()+
scale_color_gradient(low = "lightblue", high = "darkblue")
#> Warning: Removed 9 rows containing missing values (geom_text).
Created on 2020-04-22 by the reprex package (v0.3.0)
Fill geom_point() based on two conditions: either on column value, or if value is 0
Build a new variable after value
. This new variable will take two values (0,1)
. Use that as an aes()
for color
.
Here an example:
library(ggplot2)
library(dplyr)
First create new_var
according to your needs:
# new_var is 0 if wt>5, otherwise is 1
mtcars <- mtcars %>%
mutate(new_var = ifelse(wt>5, 0, 1))
#mutate(new_var = ifelse(value==0, 0, 1)) # in your case do this instead
Plot using new_var
as a new aesthetics:
ggplot(mtcars, aes(x=mpg, y=wt)) +
geom_point(aes(size=wt, color = as.factor(new_var))) + # add as.factor() to color accordingly
scale_color_manual(values=c("gray", "black")) # custom coloring the points
#+ guides(color=FALSE) # this removes the color legend
Of course you can remove later the second legend and do other improvements.
Another example, with a different shape
:
ggplot(mtcars, aes(x=mpg, y=wt)) +
geom_point(aes(size=wt, color = as.factor(new_var)), shape=21, fill="black") +
scale_color_manual(values=c("red", "black")) +
guides(color=FALSE)
if else condition in ggplot to add an extra layer
What you are seeing is a syntax error. The most robust way I can think of is:
tmp.data<-c(1,2,3)
if(tmp.data[1]!="no value") {
p = p + geom_point()
}
p + geom_line()
So you compose the object p
in a sequence, only adding geom_point()
when the if statements yields TRUE
.
Related Topics
How to Manually Change the Key Labels in a Legend in Ggplot2
Deleting Every N-Th Row in a Dataframe
Replace Specific Values Based on Another Dataframe
How to Return 5 Topmost Values from Vector in R
Find Consecutive Values in Vector in R
Create Url Hyperlink in R Shiny
Ggplot2: Using Gtable to Move Strip Labels to Top of Panel for Facet_Grid
Dplyr::Do() Requires Named Function
Union of Intersecting Vectors in a List in R
R Programming: Cache the Inverse of a Matrix
Count the Number of Non-Zero Elements of Each Column
Use Loop to Generate Section of Text in Rmarkdown
Reading Information from a Password Protected Site
Rounding Time to Nearest Quarter Hour
Change the Number of Breaks Using Facet_Grid in Ggplot2
Subset Data.Table by Logical Column