Home > database >  How to show values in ggplot instead of geom
How to show values in ggplot instead of geom

Time:05-20

I have a dataset that looks as follows:

cluster <- c(rep(c(1:4), 2))
score <- c(1.3, 7.2, 4.9, 7.5, 6.8, 4.1, 9.9, 5.8)
x_axis <- c(rep("indicator1", 4), rep("indicator2", 4))

dt <- data.table(cluster, score, x_axis)

and I am plotting the score for each indicator by cluster:

ggplot()  
  geom_point(data=dt, aes(x=x_axis, y=score))

I would like to do one of the following:

  1. Replace the dots in the plot with cluster number (or put the number next to the dot), or
  2. Use different shapes/color for each cluster and add a legend below the plot, indicating to which cluster does each shape correspond to.

How can I do this?

CodePudding user response:

Here's both options. To use shape for the clusters, pass factor(cluster) to the shape aesthetic of geom_point.

ggplot(dt, aes(x_axis, score))  
  geom_point(aes(shape = factor(cluster)), size = 4)  
  theme_bw(base_size = 16)  
  labs(shape = 'Cluster')

enter image description here

If you just want labels at each point, you don't need the shape aesthetic, but you might want to draw in some white circles behind your text:

ggplot(dt, aes(x_axis, score))  
  geom_point(size = 8, shape = 21, fill = 'white')  
  geom_text(aes(label = cluster), size = 5)  
  theme_bw(base_size = 16)

enter image description here

If you're writing to a format where colors are permissable, I think the clearest way to label the clusters would be to map them to the color aesthetic:

ggplot(dt, aes(x_axis, score))  
  geom_point(aes(color = factor(cluster)), size = 8)  
  theme_bw(base_size = 16)  
  scale_color_brewer(palette = 'Set1')  
  labs(color = 'Cluster')

enter image description here

  • Related