r - 如何更改 ggplot 气泡图中轮廓的颜色?

标签 r ggplot2 bubble-chart

我正在 ggplot2 中制作气泡图,并且想要更改气泡的轮廓(以符合工作的格式指南)。我的问题是我使用颜色对变量进行分组,并且我使用自定义调色板(同样,用于工作)。理想情况下,我想在气泡上加上黑色边框。

这是我的数据框:

mmur <- structure(list(Medgrowth = c(-1.02232983588915, 3.01155115511551,-0.220617729642996, 1.96506550218342, 0.943970767356888, 0.810810810810807,0.0166694449074782, 0.21064457239153, 0.0876731544801004, 0.132216835610393,0.370644922164558,0.23378141437756, 1.27810650887574, 0.42301184433164,0.394880174291941, 0.54216172568924, 1.32690882134916, 0.499722376457527,-0.108885017421599), Medunemp = c(4.430550475, 2.5060469975,4.1239796475, 2.0585977455, 3.846659243, 3.1792594425, 4.0033450105,6.0882984255, 3.091889808,3.7462810695, 2.4038147815, 3.0065393475,2.3331894185, 4.9482480125, 2.0955470885, 1.616694725, 1.873037069,3.060170157, 3.0131425595), Empsize = c(324.2,270.6, 962.1,149, 962.4, 421.1, 1197.8, 777.8, 552.8, 234.8, 421.1, 203.2,915.7, 396.1, 685.9, 904.5, 1366.9, 215.4, 440.5), Eduratio = c(0.1,0.2, 0.1, 0.2, 0.1, 0.2, 0.1, 0.1, 0.1, 0.3, 0.3, 0.2, 0.5, 0.2,0.3, 0.6, 0.4, 0.2, 0.1), Names = structure(c(3L, 12L, 11L, 7L,5L, 19L, 17L, 1L, 18L, 10L, 8L, 16L, 14L, 2L, 15L, 6L, 9L, 4L,13L), .Label = c("Accom", "Admin","Agric", "Arts.", "Const","Educa", "Elect", "Finan", "Healt","Infor","Manuf","Minin","Other", "Profe", "Publi", "Renta", "Retai", "Trans", "Whole"), class = "factor"), colour1 = structure(c(6L, 5L, 6L, 5L, 6L,5L, 6L, 6L, 6L, 4L, 4L, 5L, 2L, 5L, 4L, 1L, 3L, 5L, 6L), .Label = c("#8C2D04","#CC4C02", "#EC7014", "#FE9929","#FEC44F", "#FEE391"), class = "factor")), .Names = c("Medgrowth","Medunemp", "Empsize", "Eduratio", "Names", "colour1"), row.names = c("Agric","Minin", "Manuf", "Elect", "Const", "Whole", "Retai", "Accom","Trans", "Infor", "Finan", "Renta", "Profe", "Admin", "Publi","Educa", "Healt", "Arts.", "Other"), class = "data.frame")

这是我的情节代码:

bbubc1 <- ggplot(mmur, aes(x = Medgrowth, y = Medunemp, size = Empsize, label = Names, colour = colour1)) +
    geom_point() + 
    scale_size(range = c(5, sqrt(max(mmur$Empsize)/min(mmur$Empsize)*5^2)), name = "Employment in\n2012 (thousands)") +
    geom_text(size = 4, colour = "black", vjust = -1) + 
    scale_colour_manual(values = levels(mmur$colour1), name = "Per cent with\ntertiary degree", label = c(60, 50, 40, 30, 20, 10)) + 
    xlab("Median employment growth rate 2001 - 2012") +
    ylab("Median unemployment rate 2001 - 2012") + 
    opts(axis.text.x=theme_text(angle=0, hjust=0, size = 16)) + 
    opts(axis.text.y=theme_text(angle=0, hjust=0, size = 16)) +
    opts(axis.title.x=theme_text(size = 16)) + 
    opts(legend.title = theme_text(size = 16)) + 
    opts(axis.title.y=theme_text(size = 16, angle = 90)) + 
    geom_vline(colour = I("grey")) + 
    geom_hline(colour = I("grey")) + 
    ylim(c(0,7))

剧情如下: The plot I want to edit

最佳答案

R 的一些绘图字符允许内部填充和边缘颜色,因此使用这些形状之一来绘制点,并用 fill 替换您的 colour 美学。 ,应该完成这项工作:

ggplot(mmur, aes(x = Medgrowth, y = Medunemp, size = Empsize, label = Names, fill = colour1)) +
  geom_point(shape=21, colour='black') + 
  scale_size(range = c(5, sqrt(max(mmur$Empsize)/min(mmur$Empsize)*5^2)), name = "Employment in\n2012 (thousands)") +
  geom_text(size = 4, colour = "black", vjust = -1) + 
  scale_fill_manual(values = levels(mmur$colour1), name = "Per cent with\ntertiary degree", label = c(60, 50, 40, 30, 20, 10)) + 
  xlab("Median employment growth rate 2001 - 2012") +
  ylab("Median unemployment rate 2001 - 2012") + 
  opts(axis.text.x=theme_text(angle=0, hjust=0, size = 16)) + 
  opts(axis.text.y=theme_text(angle=0, hjust=0, size = 16)) +
  opts(axis.title.x=theme_text(size = 16)) + 
  opts(legend.title = theme_text(size = 16)) + 
  opts(axis.title.y=theme_text(size = 16, angle = 90)) + 
  geom_vline(colour = I("grey")) + 
  geom_hline(colour = I("grey")) + 
  ylim(c(0,7))

运行example(points)并转到第三个图,看看哪些形状可以像这样填充,即形状21:25。

关于r - 如何更改 ggplot 气泡图中轮廓的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12830160/

相关文章:

r - 如何使用 geom_contour_fill 制作离散渐变色条?

R 根据需要创建一个覆盖美国 basemap 和其他空间图层的空间气泡图

python - 将高维 R 数据集加载到 Pandas DataFrame

r - 如果变量不存在,则将变量添加到数据集中

r - 我如何使用 pivot wider 按组进行汇总

json - JSON 文件中的 "NA"转换为 NA 逻辑

R ggplot 改变堆积条形图中一个变量的颜色

r - 为 R 中的同一变量绘制两个不同的 x Axis

javascript - dc.js 气泡图上仅显示结束刻度线

python - 气泡图标题和颜色