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

cbjzeqam  于 2023-06-19  发布在  其他
关注(0)|答案(2)|浏览(95)

对于面板数据的处理,我是个新手,如何只绘制所需列的选定部分?
例如:

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

使用这段代码,我将在图中绘制出我不想要的每一个国家的线。
我应该写些什么来只绘制我想要的选定的几个国家(例如,中国、美国、澳大利亚),而这些国家不需要我事先转换数据?

z6psavjg

z6psavjg1#

您可以使用[%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更改为包含您所在国家的列的名称。

gojuced7

gojuced72#

如何将数据集中名为year的列从数字数据转换为2004 - 2021年多个国家的趋势图

相关问题