Search code examples
rggplot2panel-data

How do I plot only the selected countries from a panel data in R?


New at working with panel data, how do I plot only the selected parts of the column I want?

For example:

my.ts.plot <- ggplot(summary.df, aes(x = Year)) +
  geom_line(aes(y = Trade.to.GDP, colour = Code)) +  
  scale_x_continuous(breaks = seq(min(summary.df$Year), max(summary.df$Year), 5)) +
  theme(axis.text.x = element_text(angle = 90, vjust=0.5)) + 
  scale_color_discrete(name = "Countries by Code") + 
  labs(title = "Trade to GDP Ratio by COuntry (1970-2017)",
       y = "Trade to GDP Ratio (Export + Import/ GDP)", 
       x = "")

my.ts.plot

Using this code, I'll end up plotting lines for every single country in the graph which I don't want.

What should I write to plot only the selected few countries that I want (say, CHN, USA, AUS) that does not involve me transforming the data beforehand?


Solution

  • You can subset summary.df directly in your call to ggplot using [ and %in%:

    my.ts.plot <- ggplot(summary.df[summary.df$Country %in% c("CHN","USA","AUS"),], aes(x = Year)) +
      geom_line(aes(y = Trade.to.GDP, colour = Code)) +  
      scale_x_continuous(breaks = seq(min(summary.df$Year), max(summary.df$Year), 5)) +
      theme(axis.text.x = element_text(angle = 90, vjust=0.5)) + 
      scale_color_discrete(name = "Countries by Code") + 
      labs(title = "Trade to GDP Ratio by COuntry (1970-2017)",
           y = "Trade to GDP Ratio (Export + Import/ GDP)", 
           x = "")
    

    You will need to change $Country to whatever the column that contains your countries is named.