【问题标题】:R bubble plot using ggplot manually selecting the colour and axis namesR气泡图使用ggplot手动选择颜色和轴名称
【发布时间】:2018-07-04 17:27:15
【问题描述】:

我使用 ggplot 创建气泡图。使用此代码:

ggplot(df, aes(x = order, y = mean, size = n, fill = name)) +
  geom_point(shape = 21) +
  theme_bw() +
  theme() + 
  scale_size(range = c(1, 50)) + 
  ylim(0,100)

除了两件事之外,它运行良好:

  1. 对于每个名称(填充),我想手动指定使用的颜色(通过将名称映射到颜色的数据框) - 这是为了在多个数字之间提供一致性。
  2. 我想用 y 上的数字代替文本标签(由于排序问题,我从一开始就无法使用文本标签)

我已经尝试了几种分别使用 scale_color_manual() 和 scale_y_continuous 的方法,但我无处可去!任何帮助将不胜感激!

谢谢

【问题讨论】:

标签: r ggplot2


【解决方案1】:

由于您没有指定示例 df,因此我创建了自己的示例。

要手动指定颜色,您必须使用带有命名向量的scale_fill_manual 作为values 的参数。

编辑 2

这似乎可以满足您的需求。我们使用scale_y_continuousbreaks 参数指定位置向量,而 labels 参数指定应出现在这些位置的标签。由于我们在创建数据框时已经创建了向量,因此我们只需将这些向量作为参数传递。

ggplot(df, aes(x = order, y = mean, size = n, fill = name)) +
  geom_point(shape = 21) +
  scale_fill_manual(values = gcolors) +
  scale_size(limits = c(min(df$n), max(df$n))) +
  scale_y_continuous(breaks = mean, labels = order_label)

编辑 1

根据您的评论,您似乎想要标记这些圈子。一种选择是使用geom_text。代码如下。您可能需要尝试使用 nudge_y 的值来获得正确的位置。

order <- c(1, 2)
mean <- c(0.75, 0.3)
n <- c(180, 200)
name <- c("a", "b")
order_label <- c("New York", "London")
df <- data.frame(order, mean, n, name, order_label, stringsAsFactors = FALSE)

color <- c("blue", "red")
name_color <- data.frame(name, color, stringsAsFactors = FALSE)
gcolors <- name_color[, 2]
names(gcolors) <- name_color[, 1]

ggplot(df, aes(x = order, y = mean, size = n, fill = name)) +
  geom_point(shape = 21) +
  geom_text(aes(label = order_label), size = 3, hjust = "inward",
            nudge_y = 0.03) +
  scale_fill_manual(values = gcolors) +
  scale_size(limits = c(min(df$n), max(df$n))) +
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank()) +
  ylab(NULL)

原答案

不清楚您所说的“用 y 上的数字代替文本标签”是什么意思。在下面的示例中,我使用 scales::percent_format() 函数将 y 轴格式化为百分比。这和你想要的一样吗?

order <- c(1, 2)
mean <- c(0.75, 0.3)
n <- c(180, 200)
name <- c("a", "b")
df <- data.frame(order, mean, n, name, stringsAsFactors = FALSE)

color <- c("blue", "red")
name_color <- data.frame(name, color, stringsAsFactors = FALSE)
gcolors <- name_color[, 2]
names(gcolors) <- name_color[, 1]

ggplot(df, aes(x = order, y = mean, size = n, fill = name)) +
  geom_point(shape = 21) +
  scale_fill_manual(values = gcolors) +
  scale_size(limits = c(min(df$n), max(df$n))) +
  scale_y_continuous(labels = scales::percent_format())

【讨论】:

  • 感谢您的着色效果完美。为糟糕的解释道歉。使用您的示例,我希望将 y 轴上的 1 重新标记为 london,将 y 轴上的 2 重新标记为 NYC
  • @Rob 我已经编辑了我的答案以提供另一个选项。这对你有用吗?
  • 对不起,我没有解释清楚,你的修正标记了气泡,但我希望用字符标签替换 Y 轴本身的数值
  • @Rob 我做了另一个编辑 - 这应该给你想要的。如果可行,请接受答案。
【解决方案2】:

感谢您的所有帮助,这非常有效:

ggplot(df, aes(x = order, y = mean, size = n, fill = name)) +
  geom_point(shape = 21) +
  scale_fill_manual(values = gcolors) +
  scale_size(limits = c(min(df$n), max(df$n))) +
  scale_x_continuous(breaks = order, labels = order_label)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-31
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 2018-09-15
    • 2016-05-14
    相关资源
    最近更新 更多