【问题标题】:我们可以在一个 ggplot 图例中显示两个属性吗?
【发布时间】:2022-01-12 08:12:18
【问题描述】:

阅读我发现的序数数据图this plot:

我发现图例是多余的,因为例如 10 的小频率总是一个小方块一个浅方块,依此类推。因此,我想用这样的图例创建一个情节(油漆解决方案):

重新创建情节的代码

# DATA
importance <- rep(1:5, times = c(30, 42, 75, 93, 60))
often <- c(rep(1:5, times = c(15, 07, 04, 03, 01)), #n=30, importance 1
           rep(1:5, times = c(10, 14, 12, 03, 03)), #n=42, importance 2
           rep(1:5, times = c(12, 23, 20, 13, 07)), #n=75, importance 3
           rep(1:5, times = c(16, 14, 20, 30, 13)), #n=93, importance 4
           rep(1:5, times = c(12, 06, 11, 17, 14))) #n=60, importance 5
running.df <- data.frame(importance, often)
runningcounts.df <- as.data.frame(table(importance, often))

# PLOT
theme_nogrid <- function (base_size = 12, base_family = "") {
  theme_bw(base_size = base_size, base_family = base_family) %+replace% 
    theme(panel.grid = element_blank())   
}

ggplot(runningcounts.df, aes(importance, often)) +
  geom_point(aes(size = Freq, color = Freq, stat = "identity", position = "identity"), shape = 15) +
  scale_size_continuous(range = c(3,15)) + 
  scale_color_gradient(low = "white", high = "black") +
  theme_nogrid()

【问题讨论】:

  • 嗨。您的代码不包含runningcount.df,您是不是忘了添加一些内容?
  • @Maël 添加到问题中。谢谢。

标签: r ggplot2 legend


【解决方案1】:

如果你想快速修复,这应该可以工作

# PLOT
theme_nogrid <- function (base_size = 12, base_family = "") {
  theme_bw(base_size = base_size, base_family = base_family) %+replace% 
    theme(panel.grid = element_blank())   
}

ggplot(runningcounts.df, aes(importance, often)) +
  geom_point(aes(size = Freq, color = Freq, stat = "identity", position = "identity"), shape = 15) +
  scale_size_continuous(range = c(3,15)) + 
  scale_color_gradient(low = "white", high = "black") +
  theme_nogrid()+
  guides( colour = guide_legend()) #added guides()

【讨论】:

  • 和朴的回答类似。但是正如您的回答所暗示的那样,不需要像 Parks 回答中那样指定 `guides(color = guide_legend(), size = guide_legend()) +`。我需要了解更多关于指南的信息。谢谢(+1)
【解决方案2】:

您可以尝试使用guides,在您的代码中添加guides(color = guide_legend(), size = guide_legend()) 行。

ggplot(runningcounts.df, aes(importance, often)) +
  geom_point(aes(size = Freq, color = Freq, stat = "identity", position = "identity"), shape = 15) +
  scale_size_continuous(range = c(3,15)) + 
  scale_color_gradient(low = "white", high = "black") +
  guides(color = guide_legend(), size = guide_legend()) +
  theme_nogrid()

【讨论】:

    猜你喜欢
    • 2021-11-15
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多