如何显示趋势线的图例?

时间:2017-11-29 18:39:36

标签: r ggplot2 visualization legend

问题

我似乎无法显示使用stat_smooth()生成的趋势线。在我使用参数show.legend = T之前,我有一个如下图:

IMAGE

添加参数后,我得到了类似的结果:

IMAGE2

但是你看,我想分别显示趋势线图例,如下所示:

IMAGE3

我如何实现这一目标?我的源代码在这里,如果你需要它们(我很感激,如果你可以帮我截断代码使其更简洁):

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. 如何将整个情节转换为可以嵌入网站的互动情节?
  4. 修改 谢谢@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(...)
    ...
    

    谢天谢地,它显示了趋势线的传奇。但仍然不理想:

    enter image description here

    如何改进代码?

1 个答案:

答案 0 :(得分: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)
                          )
      )
    )

enter image description here