数据从直方图中丢失

时间:2019-05-16 14:51:33

标签: r ggplot2 histogram

我正在尝试制作一个简单的直方图,以按日期显示进入系统的鱼的数量。鱼正在通过两个位置进入系统,我想为每个站点的直方图的条形着色。但是,当我添加填充美学时,7月7日(来自A站的唯一条目)的数据将被删除。这就是没有填充美学的外观...

enter image description here

这就是填充... enter image description here

这是我没有填写的代码:

  geom_histogram(aes(monthday), stat = 'count') +
  theme_bw() + labs(x = "Date", y = "Fish Entry") + theme(axis.text = element_text( size = 12))

并且使用:

  geom_histogram(aes(monthday, fill = Station2), stat = 'count') +
  theme_bw() + labs(x = "Date", y = "Fish Entry") + theme(axis.text = element_text( size = 12))

数据框“第一”有两列:monthday(POSIXct,y-m-d)和Station2(因子A,G)

这是我的ASCII数据:

structure(list(monthday = structure(c(1529539200, 1529625600, 
1529625600, 1529712000, 1529712000, 1529798400, 1529798400, 1529884800, 
1529884800, 1529971200, 1529971200, 1529971200, 1529971200, 1529971200, 
1529971200, 1529971200, 1529971200, 1529971200, 1530057600, 1530057600, 
1530057600, 1530057600, 1530057600, 1530057600, 1530057600, 1530144000, 
1530230400, 1530230400, 1530316800, 1530403200, 1530489600, 1530921600
), class = c("POSIXct", "POSIXt"), tzone = "GMT"), Station2 = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L), .Label = c("A", 
"G"), class = "factor")), class = "data.frame", row.names = c(43424L, 
43504L, 43774L, 43778L, 44055L, 44327L, 44328L, 44511L, 44677L, 
44796L, 44830L, 44921L, 44942L, 45373L, 45394L, 45449L, 45817L, 
45825L, 46104L, 46106L, 46625L, 47214L, 47229L, 47389L, 47544L, 
50186L, 52027L, 54694L, 55437L, 61670L, 67598L, 96186L))

1 个答案:

答案 0 :(得分:1)

与此同时,一个快速的技巧是:

ggplot(first,aes(x=as.Date(monthday), fill = Station2))+
geom_histogram(stat = 'count') +
theme_bw() + labs(x = "Date", y = "Fish Entry") + 
theme(axis.text = element_text( size = 12))

enter image description here

我认为您需要as.Date(monthday),因为当您将monthday更改为Date时,它将输入视为一整天(POSIXct可能会将其视为当天的一小部分)。因为x轴是连续的,所以A的条形图的宽度是如此之窄,以至于当您使用fill = Station2时,它不会显示在绘图上。无法100%确定fill为何导致A消失的原因,但是这种解决方法应该会有所帮助。