【问题标题】:How to show the legend of a trend line?如何显示趋势线的图例?
【发布时间】:2017-11-29 18:39:36
【问题描述】:

问题

我似乎很难显示使用stat_smooth() 生成的趋势线。在我使用参数show.legend = T 之前,我的图表如下所示:

添加参数后,我得到了这样的结果:

但是你看,我想单独显示趋势线图例,像这样:

我如何实现这一目标?如果您需要,我的源代码在这里(如果您能帮我截断代码以使其更简洁,我将不胜感激):

library(ggplot2)
library(ggrepel)
library(ggthemes)
library(scales)
library(plotly)
library(grid)
library(extrafont)

# read data
econ <- read.csv("https://raw.githubusercontent.com/altaf-ali/ggplot_tutorial/master/data/economist.csv")
target_countries <- c("Russia", "Venezuela", "Iraq", "Myanmar", "Sudan",
  "Afghanistan", "Congo", "Greece", "Argentina", "Brazil",
  "India", "Italy", "China", "South Africa", "Spane",
  "Botswana", "Cape Verde", "Bhutan", "Rwanda", "France",
  "United States", "Germany", "Britain", "Barbados", "Norway", "Japan",
  "New Zealand", "Singapore")

econ$Country <- as.character(econ$Country)
labeled_countries <- subset(econ, Country %in% target_countries)
vector <- as.numeric(rownames(labeled_countries))

econ$CountryLabel <- econ$Country
econ$CountryLabel[1:173] <- ''
econ$CountryLabel[c(labeled_countries$X)] <- labeled_countries$Country

# Data Visualisation
g <- ggplot(data = econ, aes(CPI, HDI)) +
  geom_smooth(se = FALSE, method = 'lm', colour = 'red', fullrange = T, formula = y ~ log(x), show.legend = T) +
  geom_point(stroke = 0, color = 'white', size = 3, show.legend = T)

g <- g + geom_point(aes(color = Region), size = 3, pch = 1, stroke = 1.2)

g <- g + theme_economist_white()

g <- g + scale_x_continuous(limits = c(1,10), breaks = 1:10) +
   scale_y_continuous(limits = c(0.2, 1.0), breaks = seq(0.2, 1.0, 0.1)) +
   labs(title = 'Corruption and human development',
        caption='Source: Transparency International; UN Human Development Report')


g <- g + xlab('Corruption Perceptions Index, 2011 (10=least corrupt)') +
  ylab('Human Development Index, 2011 (1=best)')

g <- g + theme(plot.title = element_text(family = 'Arial Narrow', size = 14, margin = margin(5, 0, 12, 0)),
           plot.caption = element_text(family = 'Arial Narrow', hjust = 0, margin=margin(10,0,0,0)),
           axis.title.x = element_text(family = 'Arial Narrow', face = 'italic', size = 8, margin = margin(10, 0, 10, 0)),
           axis.title.y = element_text(family = 'Arial Narrow', face = 'italic', size = 8, margin = margin(0, 10, 0, 10)),
           plot.background = element_rect(fill = 'white'),
           legend.title = element_blank()
) + theme(legend.background = element_blank(),
         legend.key = element_blank(),
         legend.text = element_text(family = 'Arial Narrow', size = 10)) +
          guides(colour = guide_legend(nrow = 1))

g <- g + geom_text_repel(data = econ, aes(CPI, HDI, label = CountryLabel), family = 'Arial Narrow',
                      colour = 'grey10', force = 8, point.padding = 0.5, box.padding = 0.3,
                      segment.colour = 'grey10'
                      )

g
grid.rect(x = 1, y = 0.996, hjust = 1, vjust = 0, gp = gpar(fill = '#e5001c', lwd = 0))
grid.rect(x = 0.025, y = 0.91, hjust = 1, vjust = 0, gp = gpar(fill = '#e5001c', lwd = 0))

奖金申请

作为一个审美水平很高的人,我想知道:

  1. 如何使国家标签分段直?请参阅第三张图片,注意“中国”的线段不是直的。
  2. 如何排列我的国家/地区标签,使它们不会在散点和趋势线上重叠? (我查阅了this Stack Overflow 的帖子,从我的代码中可以看出,我为不需要的国家/地区创建了空字符串。但是,重叠仍然存在)
  3. 如何将整个情节转换为可嵌入网站的交互式情节?

编辑: 感谢@aosmith 提供有用的建议。我关注this post 并尝试override.aes 我的趋势线。这是我在#Data Visualisation 会话中添加的内容:

g <- ggplot(data=econ, aes(CPI,HDI))+
  geom_smooth(se = FALSE, method = 'lm', aes(group = 1, colour = "Trendline"),fullrange=T, linetype=1,formula=y~log(x))+
  scale_colour_manual(values = c("purple", "green", "blue", "yellow",  "magenta","orange", "red"),
                      guides (colour = guide_legend (override.aes = list(linetype = 1)))

                      )+
  geom_point(...)
...

谢天谢地,它显示了趋势线图例。但仍然不理想:

如何改进代码?

【问题讨论】:

  • This answer 可能会帮助您开始您的传奇问题。
  • 谢谢。我之前也参考过这个帖子。但是,我想保持散点的颜色不变。看来我必须使用帖子提供的方法手动定义所有图例的颜色。
  • @aosmith,我重做方法并针对我的情况进行了修改scale_colour_manual(values = c("purple", "green", "blue", "yellow", "magenta","orange", "red"), guide = guide_legend(override.aes = list( linetype = c(rep("blank", 7), "dashed") ))。它没有在图例区域显示我的趋势线,只是改变了其他点的颜色。
  • 您可以更新您的问题以显示您尝试过的所有代码(我猜您将 color 添加到 geom_smoothaes 中,如链接答案中所示,因此该行应该显示在图例中)。在您的评论override.aes 代码中,看起来您定义了 8 种线型,但您只有 7 个图例元素(6 个区域加上线)。这可能会导致问题。
  • @aosmith 你好。我根据您的建议更新了我的问题和代码。虽然还有一些问题,但我想我越来越近了。

标签: r ggplot2 visualization legend


【解决方案1】:

问题出在guides 语句中。这是您的代码的数据可视化部分,有些固定:

# Data Visualisation
g <- ggplot(data = econ, aes(CPI, HDI)) +
  geom_smooth(se = FALSE, method = 'lm', aes(group = 1, colour = "Trendline"), fullrange=T, linetype=1, formula=y~log(x)) +
  geom_point(stroke = 0, color = 'white', size = 3, show.legend = T) +
  scale_colour_manual(values = c("purple", "green", "blue", "yellow", "magenta", "orange", "red"))


g <- g + geom_point(aes(color = Region), size = 3, pch = 1, stroke = 1.2)

g <- g + theme_economist_white()

g <- g + scale_x_continuous(limits = c(1,10), breaks = 1:10) +
  scale_y_continuous(limits = c(0.2, 1.0), breaks = seq(0.2, 1.0, 0.1)) +
  labs(title = 'Corruption and human development',
       caption='Source: Transparency International; UN Human Development Report')


g <- g + xlab('Corruption Perceptions Index, 2011 (10=least corrupt)') +
  ylab('Human Development Index, 2011 (1=best)')

g <- g + theme(plot.title = element_text(family = 'Arial Narrow', size = 14, margin = margin(5, 0, 12, 0)),
               plot.caption = element_text(family = 'Arial Narrow', hjust = 0, margin=margin(10,0,0,0)),
               axis.title.x = element_text(family = 'Arial Narrow', face = 'italic', size = 8, margin = margin(10, 0, 10, 0)),
               axis.title.y = element_text(family = 'Arial Narrow', face = 'italic', size = 8, margin = margin(0, 10, 0, 10)),
               plot.background = element_rect(fill = 'white'),
               legend.title = element_blank()
) + theme(legend.background = element_blank(),
          legend.key = element_blank(),
          legend.text = element_text(family = 'Arial Narrow', size = 10))

g <- g + geom_text_repel(data = econ, aes(CPI, HDI, label = CountryLabel), family = 'Arial Narrow',
                         colour = 'grey10', force = 8, point.padding = 0.5, box.padding = 0.3,
                         segment.colour = 'grey10'
)

g + guides(colour = guide_legend(nrow = 1,
      override.aes = list(linetype = c(rep("blank", 6), "solid"),
                          shape = c(rep(1, 6), NA)
                          )
      )
    )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多