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

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

刚开始使用面板数据,如何只绘制我想要的列的选定部分?

例如:

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

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

我应该写什么来只绘制我想要的选定的几个国家(比如中国、美国、澳大利亚)而不涉及我事先转换数据?

您可以使用 [%in%:

在对 ggplot 的调用中直接子集 summary.df
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 更改为包含您所在国家/地区的列的名称。