简体   繁体   中英

How to add a custom edge color to a bubble plot using ggplot2

My code is listed below. I have very specific colors that I must use at my work. However, I am trying to get a red edge around all of my bubbles. I have not been able to figure this out. I would think the edge should be about 1.25pts in size as well. Any help with this would be wonderful.

ggplot(data, aes(x = Date_Id, 
                 y = Level.Of.Difficulty, size = Savings), show.legend = F) +
  geom_point(aes(color = Source, alpha = 0.6)) +
  geom_text(aes(label = Initiative), size = 4) +
  scale_size_continuous(range = c(1, 75)) +
  scale_y_discrete(limits = c("Low", "", "", "", "", "", "", "", "", "High")) +
  scale_x_discrete(limits = c("","","Sep-18","","","","","","",
                              "Oct-18", "","","","","","",
                              "Nov-18", "","","","","","",
                              "Dec-18", "","","","","","",
                              "Jan-19", "","","","","","",
                              "Feb-19", "","","","","","",
                              "Mar-19", "","","","","","",
                              "Apr-19", "","","","","","",
                              "May-19", "","","","","","",
                              "Jun-19", "","","","","","", "Jul-19")) +
  labs(x = "Date", y = "Level of Difficulty") +
  ggtitle("2019 SC Major Initiatives") + 
  scale_alpha(guide = 'none') +
  geom_vline(aes(xintercept = 33), size = 1.4) + 
  geom_hline(aes(yintercept = 5.5), size = 1.4,
               linetype = 6) +
  scale_color_manual(values = c("#008000", "#0000FF")) +
  theme(plot.title = element_text(hjust = 0.5),
        legend.position = c(0.035, 0.035), 
        legend.background = element_blank(),
        legend.title = element_blank(),
        legend.text = element_text(size = 12),
        panel.background = element_blank(),
        axis.line = element_line(color = "black")) +
  guides(size = F)

I am expecting the colors to remain the same, but just to add a red edge to each bubble point.

Here is some sample data:

Source,Initiative,Date,Date_Id,Month_ID,Level Of Difficulty,Savings
A,Item 1,9/1/2018,1,1,9,295210
B,Item 2,9/5/2018,2,1,5,75000
A,Item 3,9/5/2018,2,1,6.5,123731.0192
B,Item 4,9/10/2018,3,1,8.5,224669
A,Item 5,9/15/2018,4,1,2,55031.13936
B,Item 6,4/5/2019,50,8,3,3780.4376
A,Item 7,4/5/2019,51,8,7.25,300000
B,Item 8,4/10/2019,52,8,6,179959
A,Item 9,4/10/2019,52,8,9,51377.61048
B,Item 10,1/1/2019,30,5,7.5,1066667
A,Item 11,1/5/2019,31,5,6.5,1000000
B,Item 12,1/10/2019,32,5,8.5,425000
A,Item 13,1/10/2019,32,5,9.5,68112.1856
B,Item 14,1/20/2019,34,5,3,21753.416
A,Item 15,1/20/2019,34,5,8,374608

Using a different shape along with adjusting stroke and switching fill with color seems to work:

ggplot(data, aes(x = Date_Id, 
                 y = Level.Of.Difficulty, size = Savings), show.legend = F) +
  geom_point(aes(fill = Source, alpha = 0.6), shape = 21, color = "red", stroke = 1.25) +
  geom_text(aes(label = Initiative), size = 4) +
  scale_size_continuous(range = c(1, 75)) +
  scale_y_discrete(limits = c("Low", "", "", "", "", "", "", "", "", "High")) +
  scale_x_discrete(limits = c("","","Sep-18","","","","","","",
                              "Oct-18", "","","","","","",
                              "Nov-18", "","","","","","",
                              "Dec-18", "","","","","","",
                              "Jan-19", "","","","","","",
                              "Feb-19", "","","","","","",
                              "Mar-19", "","","","","","",
                              "Apr-19", "","","","","","",
                              "May-19", "","","","","","",
                              "Jun-19", "","","","","","", "Jul-19")) +
  labs(x = "Date", y = "Level of Difficulty") +
  ggtitle("2019 SC Major Initiatives") + 
  scale_alpha(guide = 'none') +
  geom_vline(aes(xintercept = 33), size = 1.4) + 
  geom_hline(aes(yintercept = 5.5), size = 1.4,
             linetype = 6) +
  scale_fill_manual(values = c("#008000", "#0000FF")) +
  theme(plot.title = element_text(hjust = 0.5),
        legend.position = c(0.035, 0.035), 
        legend.background = element_blank(),
        legend.title = element_blank(),
        legend.text = element_text(size = 12),
        panel.background = element_blank(),
        axis.line = element_line(color = "black")) +
  guides(size = F)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM