I am trying to make these plots by dodging points. The points radius differ by size. Ideally, there have to be four points in each "coordinate" as indicated by the dataframe. The didge does not seem to work for two of the points. I tried changing the width
to 5 but I can't see the points. Any clue on what's going on?
library(ggplot2)
set.seed(123)
species <- rep(c("A","B","C","D"), each = 5)
x.axis <- rep(c(0.5, 0.5, 1, 1,0.75), each = 4)
y.axis <- rep(c(0.5, 1, 1, 0.5,0.75), each = 4)
value <- c(1,2,10,3,4,5,4,3,2,3,6,5,10,4,5,17,1,10,13,3)
data <- data.frame(specie,value, x.axis, y.axis)
# 2D plots
ggplot(data, aes(x = x.axis, y = y.axis, color = value, size = value))
theme_classic()
geom_point(aes(fill = species, color = species), alpha = 0.7,position = position_dodge(width = 0.05))
theme(text=element_text(size=10, family="Arial", color = "black"))
theme(aspect.ratio = 10/10)
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(colour = "black", size=1))
theme(axis.title.y = element_text(margin = margin(t = 0, r = 0, b = 0, l = 0)))
CodePudding user response:
Maybe this is what you are looking for. The way your points get dodged is determined by the grouping. In your case the data by default gets grouped by the variable mapped on the color
aes, i.e. species
. Hence, at each position or coordinate only points with different species
get dodged.
As far as I understand your question you want to dodge all points at the same position or coordinate instead. To this end you could add an id
column to your dataset by position, i.e. as we have four points at each position the points are assigned an id
running from 1 to 4. This id
column could then be mapped on the group
aesthetic:
library(dplyr)
library(ggplot2)
data <- data |>
group_by(x.axis, y.axis) |>
mutate(id = row_number())
# 2D plots
ggplot(data, aes(x = x.axis, y = y.axis, size = value))
theme_classic()
geom_point(aes(color = species, group = id), alpha = 0.7, position = position_dodge(width = 0.05))
theme(text = element_text(size = 10, family = "Arial", color = "black"))
theme(aspect.ratio = 10 / 10)
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(colour = "black", size = 1)
)
theme(axis.title.y = element_text(margin = margin(t = 0, r = 0, b = 0, l = 0)))