Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 用不同长度绘制数据_R_Ggplot2 - Fatal编程技术网

R 用不同长度绘制数据

R 用不同长度绘制数据,r,ggplot2,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 我想画出每个国家和年份的数据,用销售额表示。为此,我将ggplot与geom_line()一起使用。问题是日本的行在年2002年下降

我有如下数据:

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
我想画出每个国家和年份的数据,用销售额表示。为此,我将
ggplot
geom_line()
一起使用。问题是
日本
的行在
2002年下降到0,因为该年没有
日本
的数据。我想做的只是停止2001年数据中所有不代表
值的行,而不是看到2002年下降到0

编辑: 下面是我的代码。请注意,我还有一个向量,表示我想在实际数据中使用的行的大小,只需删除
scale\u size
size
即可

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)

你能试试这个代码吗?如下图所示,我的图表中没有日本的
geom_线
下降到零

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())

这听起来很奇怪。您能否分享您的
ggplot
代码,并验证您的示例数据是否说明了问题?ggplot通常不会绘制不在数据中的点。。。当我导入您的数据并执行
ggplot(dd,aes(x=Year,y=Sales,color=Country))+geom_line()时,一切看起来都正常。正如所述,ggplot不会给您带来任何问题,但是您可以执行
df%>%过滤器(Year%ggplot(aes(x=as.factor(Year),y=Sales))+…
,使用
dplyr
软件包,但是最好查看您的代码。发布代码。在实际数据中,我有更多的内容(我有一个包含500000行的数据集),但我调整了代码中的示例以使其更简单。为什么
scale_y___连续
aes
中?是的,这也是创建可复制示例总是很好的另一个原因。通常在创建示例时,您会发现问题。谢谢,这解决了我可能希望消除的值接近零的问题!