Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change symbol and fill in scale_size_continuous()

I am trying to change the symbol type (to triangle), it’s fill (to NA), and border (to black) for scale_size_continuous() in ggplot2. I would like to keep the two legends. I can’t seem to figure it out. Here is some example data:

data <- structure(list(date = structure(c(1431388800, 1444780800, 1456876800, 
                                              1469145600, 1469664000, 1425081600, 1445299200, 1488758400, 1524960000, 
                                              1454544000, 1540512000, 1429228800, 1481587200, 1526688000, 1445904000, 
                                              1498348800, 1464825600, 1451174400, 1450310400, 1539216000, 1528934400, 
                                              1525996800, 1465084800, 1531180800), class = c("POSIXct", "POSIXt"
                                              ), tzone = "UTC"), cnt1 = c(76, 140, 99, 10, 123, 124, 160, 58, 
                                                                          47, 43, 86, 112, 26, 123, 111, 49, 65, 93, 174, 49, 5, 16, 84, 
                                                                          4), cnt2 = c(111, 200, 550, 73, 123, 184, 166, 131, 189, 136, 
                                                                                       250, 195, 26, 154, 125, 100, 168, 151, 255, 49, 200, 144, 177, 
                                                                                       4), per = c(68.4684684684685, 70, 18, 13.6986301369863, 100, 
                                                                                                   67.3913043478261, 96.3855421686747, 44.2748091603053, 24.8677248677249, 
                                                                                                   31.6176470588235, 34.4, 57.4358974358974, 100, 79.8701298701299, 
                                                                                                   88.8, 49, 38.6904761904762, 61.5894039735099, 68.2352941176471, 
                                                                                                   100, 2.5, 11.1111111111111, 47.4576271186441, 100), status = c("A", 
                                                                                                                                                                  "B", "C", "C", "B", "B", "B", "A", "A", "A", "C", "C", "C", "A", 
                                                                                                                                                                  "B", "C", "B", "C", "A", "A", "A", "B", "B", "B"), category = c("CAT3", 
                                                                                                                                                                                                                                  "CAT1", "CAT3", "CAT2", "CAT1", "CAT1", "CAT1", "CAT3", "CAT2", 
                                                                                                                                                                                                                                  "CAT3", "CAT2", "CAT1", "CAT1", "CAT1", "CAT2", "CAT2", "CAT1", 
                                                                                                                                                                                                                                  "CAT1", "CAT1", "CAT1", "CAT3", "CAT3", "CAT3", "CAT1")), .Names = c("date", 
                                                                                                                                                                                                                                                                                                       "cnt1", "cnt2", "per", "status", "category"), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                   -24L), class = c("tbl_df", "tbl", "data.frame"))

Here is the legend item that I’m trying to change (to upside triangles with no fill):

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is my plotting function:

ggplot(data, aes(x=date, y=cnt2)) + 
      scale_y_log10(breaks=c(1,10,100,1000,5000)) +
      geom_point(aes(fill=category,col=category, size=per, shape=category), alpha=0.7)+
      scale_color_manual(name = "",values=c("CAT1"="black","CAT2"="black","CAT3"="black")) + 
      scale_shape_manual(name = "",values=c('CAT1'=24, 'CAT2'=25, 'CAT3'=23))+
      scale_fill_manual(name = "",values=c("CAT1"="blue","CAT2"="red","CAT3"="green"))+
      scale_size_continuous(trans="sqrt",range = c(1, 5),breaks=c(5,10,25,50,75,100)) 
  

>Solution :

Add in guides(size = guide_legend(override.aes = list(shape = 25))) to manually set the shape in the legend:

library(ggplot2)

ggplot(data, aes(x=date, y=cnt2)) + 
  scale_y_log10(breaks=c(1,10,100,1000,5000)) +
  geom_point(aes(fill=category,col=category, size=per, shape=category), alpha=0.7)+
  scale_color_manual(name = "",values=c("CAT1"="black","CAT2"="black","CAT3"="black")) + 
  scale_shape_manual(name = "",values=c('CAT1'=24, 'CAT2'=25, 'CAT3'=23))+
  scale_fill_manual(name = "",values=c("CAT1"="blue","CAT2"="red","CAT3"="green"))+
  scale_size_continuous(trans="sqrt",range = c(1, 5),breaks=c(5,10,25,50,75,100)) +
  guides(size = guide_legend(override.aes = list(shape = 25)))

Or scale_size_continuous(..., guide = guide_legend(override.aes = list(shape = 25))) is the other possible place to amend this within the scale_ function call.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading