Xipu Li
Xipu Li

Reputation: 89

How to show the legend of a trend line?

Problem

It seems that I'm having difficulty showing the trend line that generated using stat_smooth(). Before I used argument show.legend = T, I have a graph looks like this:

IMAGE

After adding the argument, I got something like this:

IMAGE2

But you see, I want to show the trendline legend separately, like this:

IMAGE3

How do I achieve this? My source codes are here if you need them (I appreciate it if you can help me truncate the codes to make it more concise):

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))

Bonus Request

As a man of high aesthetic standard, I would like to know:

  1. How to make country-label segments not straight? Refer to the third image, notice the segment line for 'China' is not straight.
  2. How do I arrange my country labels so that they don't overlap on scatter points and the trendline? (I consulted this Stack Overflow post, and as you can see from my codes, I created empty strings for countries I don't need. However, the overlapping persists)
  3. How to convert the whole plot into an interactive plot that can be embedded on a website?

EDIT: Thanks @aosmith for helpful suggestions. I followed this post and tried to override.aes my trend line. This is what I added to the #Data Visualisation session:

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(...)
...

Thankfully it shows the trendline legend. But still not ideal:

enter image description here

How do I improve the codes?

Upvotes: 4

Views: 1557

Answers (1)

Claus Wilke
Claus Wilke

Reputation: 17810

The problem is in the guides statement. Here is the data visualization part of your code, somewhat fixed up:

# 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

Upvotes: 2

Related Questions