我如何仅从R中的面板数据中绘制选定的国家?

问题描述 投票:-1回答:1

使用面板数据的新功能,如何仅绘制所需的列的选定部分?

例如:

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

使用此代码,我最终在图表中为不需要的每个国家/地区绘制线条。

我应该写些什么来只绘制我想要的少数几个不涉及我事先转换数据的国家(例如,CHN,美国,AUS)?

r ggplot2 panel-data
1个回答
0
投票

您可以在使用summary.dfggplot来调用[时直接将%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 = "")

您需要将$Country更改为包含您所在国家/地区的列的名称。

© www.soinside.com 2019 - 2024. All rights reserved.