Search code examples
rggplot2scatter-plotbubble-chart

Draw points border in a customized plot


I have a dataset like this:

Year<-rep(2001:2005, each = 5)
name<-c("John","Ellen","Mark","Randy","Luisa")
Name<-c(rep(name,5))
Value<-sample(seq(0,25,by=1),25)
mydata<-data.frame(Year,Name,Value)

And my plot looks like this:

p <- ggplot(mydata, aes(x=Year, y=reorder(Name, desc(Name)), size = Value)) +
  geom_point(aes(colour = Value, 
                 alpha = I(as.numeric(Value > 0)))) 
p <- p +  scale_colour_viridis_c(option = "D", direction = -1,
                           limits = c(1, 25)) +
scale_size_area(guide = "none") +
  ylab("Name") + 
  theme(axis.line = element_blank(),
        axis.text.x=element_text(size=11,margin=margin(b=10),colour="black"),
        axis.text.y=element_text(size=13,margin=margin(l=10),colour="black",
                                 face="italic"),
        axis.ticks = element_blank(),
        axis.title=element_text(size=18,face="bold"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        legend.text = element_text(size=14),
        legend.title = element_text(size=18))

I would like to improve it in two ways but I couldn't figure out how.

  1. I would like to add a black border around points. I know I should use pch>20 and specify colour, but because my colours are mapped to a feature of the dataset (they depend on value, in this case), I don't know exactly how to do that. Note that value = 0 points are not plotted. Easy stratagems such as plotting bigger black points under my points seem utopic for me.
  2. I would like to change the breaks of the scale (e.g., instead of having breaks every 5, I'd like to have breaks every 2.5), but it is a continuous scale, and I'm not sure how to do that.

I am not very familiar with ggplo2, thus any help would be appreciated!


Solution

  • You can indeed use a shape >20, e.g. I use shape=21 here. Then you need to change your scale_color_ to scale_fill_, because the color is now black (it is the border of the shape).

    For breaks, you could just specify them in the scale itself. Combining both:

    ggplot(mydata, aes(x=Year, y=reorder(Name, desc(Name)), size = Value)) +
      geom_point(aes(fill = Value, 
                     alpha = I(as.numeric(Value > 0))), shape=21, color = "black")  +  
      scale_fill_viridis_c(option = "D", direction = -1,
                                     limits = c(1, 25), breaks=seq(1, 25, 2.5)) +
      scale_size_area(guide = "none") +
      ylab("Name") + 
      theme(axis.line = element_blank(),
            axis.text.x=element_text(size=11,margin=margin(b=10),colour="black"),
            axis.text.y=element_text(size=13,margin=margin(l=10),colour="black",
                                     face="italic"),
            axis.ticks = element_blank(),
            axis.title=element_text(size=18,face="bold"),
            panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
            panel.background = element_blank(),
            legend.text = element_text(size=14),
            legend.title = element_text(size=18))