Phil D
Phil D

Reputation: 193

displaying different symbols for each point within the same factor in R ggplot2

I am trying to create a plot to show the mean of calculated values within each group (organised by factors), as well as the induvidual points themselves. I have managed to do this successfully, however all the points use the same symbol. I want to have a different symbol for each of the points within each factor, and preferably use the same points in the same order for each factor.

An example version of the kind of graph I am currently making is below, however all the points within the same column use the same symbol.

I have thought about using the row number of the points to define the symbol shape, but I think there are only 25 different shapes available in the default ggplot2 package, and my real data has more than 25 points, plus I would prefer if the same points were used in each column, to keep the graph looking consistent.

Mean_list <- data.frame(Cells = factor(c("Celltype1", "Celltype2", "Celltype3", 
                        "Celltype4"), 
                        levels =c("Celltype1", "Celltype2", "Celltype3", "Celltype4")),
                        Mean = c(mean(c(1, 2, 3)), mean(c(5, 8, 4)), mean(c(9, 8 ,3)), 
                        mean(c(3, 6, 8, 5))))

values_list <- data.frame(Cells2 = rep(c("Celltype1", "Celltype2", "Celltype3", 
                         "Celltype4"), times = c(length(c(1, 2, 3)),
                         length(c(5, 8, 4)), length(c(9, 8 ,3)), 
                         length(c(3, 6, 8, 5)))), 
                         values =  c(1, 2, 3, 5, 8, 4, 9, 8, 3, 3, 6, 8, 5))

ggplot() + geom_col(data = Mean_list, aes(Cells, Mean, fill = Cells)) +
  geom_point(data = values_list, aes(Cells2, values)) 

Upvotes: 4

Views: 1616

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48211

Before plotting we may assign a number to each row in within a cell:

values_list <- values_list %>% group_by(Cells2) %>% mutate(shape = factor(seq_along(values)))

ggplot() +
  geom_col(data = Mean_list, aes(Cells, Mean, fill = Cells)) +
  geom_point(data = values_list, aes(Cells2, values, shape = shape))

enter image description here

Upvotes: 2

Related Questions