首页 > 解决方案 > 如何使用 ggplot2 将自定义边缘颜色添加到气泡图

问题描述

我的代码在下面列出。我有非常具体的颜色,我必须在工作中使用。但是,我试图在我所有的气泡周围设置一个红色边缘。我一直无法弄清楚这一点。我认为边缘的大小也应该是 1.25pts 左右。对此的任何帮助都会很棒。

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)

我希望颜色保持不变,但只是为每个气泡点添加一个红色边缘。

以下是一些示例数据:

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

标签: rggplot2

解决方案


使用不同的方法shape以及调整和stroke切换似乎有效:fillcolor

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)

在此处输入图像描述


推荐阅读