绘制不同长度的数据

时间:2018-10-08 14:20:36

标签: r ggplot2

我有如下数据:

Country Sales Year    
Germany 2000  2000
Germany 1500  2001
Germany 2150  2002
UK      1200  2000
UK      1300  2001
UK      2000  2002
Japan   500   2000
Japan   750   2001

我想绘制每个国家和地区的销售数据。为此,我将ggplotgeom_line()一起使用。问题在于,Japan的那一行在2002年的year下降为0,因为该年没有Japan的数据。我要做的只是在2001年停止数据中不代表value的所有行,而不是看到2002年下降到0。

编辑: 我的代码如下。请注意,对于要在实际数据中使用的行的大小,我也有一个向量,只需删除scale_sizesize即可消除。

ggplot(df_Filtered, aes(x = Year, y = Sales, colour = Country, scale_y_continuous(breaks = 1), size=mysize)) +  geom_line() +
    labs(x = paste("Sales per country"), y = "Sales per country", title = NULL) +
    scale_x_continuous(breaks = c(2000, 2001, 2002)) + 
    scale_size(range = c(1, 4), guide="none") +
    theme(panel.background = element_blank())
ggsave(paste("Output/", "Sales", ".png", sep = ""), width=20, height=11, limitsize = FALSE)

1 个答案:

答案 0 :(得分:1)

您可以尝试使用此代码吗?我的图表如下所示,没有日本的geom_line降为零。

df_Filtered <- data.frame(stringsAsFactors=FALSE,
     Country = c("Germany", "Germany", "Germany", "UK", "UK", "UK", "Japan",
                 "Japan"),
       Sales = c(2000L, 1500L, 2150L, 1200L, 1300L, 2000L, 500L, 750L),
        Year = c(2000L, 2001L, 2002L, 2000L, 2001L, 2002L, 2000L, 2001L)
)

mysize <- 0.1

ggplot(df_Filtered, aes(x = Year, y = Sales, colour = Country, scale_y_continuous(breaks = 1), size=mysize)) +  geom_line() +
  labs(x = paste("Sales per country"), y = "Sales per country", title = NULL) +
  scale_x_continuous(breaks = c(2000, 2001, 2002)) + 
  scale_size(range = c(1, 4), guide="none") +
  theme(panel.background = element_blank())

enter image description here

相关问题