我的数据如下:
k2=structure(list(Hour = c("17:02:00", "17:04:00", "17:07:00", "17:13:00",
"17:14:00", "17:17:00", "17:19:00", "17:22:00", "17:28:00", "17:29:00"
), Cat1 = c(300L, 304L, 272L, 171L, 271L, 376L, 284L, 177L, 218L,
284L), Cat2 = c(15L, 45L, 36L, 31L, 36L, 26L, 26L, 32L, 46L,
32L), Cat3 = c(850L, 1073L, 612L, 537L, 709L, 929L, 870L, 452L,
474L, 696L), Label = c("BA", "EL", "BA", "CI", "MO",
"BA", "EL", "BA", "CI", "RO")), .Names = c("Hour",
"Cat1", "Cat2", "Cat3", "Label"), row.names = c("163", "164",
"165", "167", "168", "169", "170", "171", "173", "174"), class = "data.frame")
我用一个简单的情节问题叠加如下:X表示时间,Y表示积累Cat1
,Cat2
,Cat3
的数量。在给定时间内,这三个数量将具有相同的Label
。
我改编了我的数据如下,但这不好,因为每只猫都有一个geom_text = Label
,而三只猫有Label
。
k2$Hour=format(k2$Hour, format='%H:%M:%S' )
meltk2 = melt(k2, id = c("Hour","Label"))
meltk2$Hour <- as.POSIXct(paste("2012-11-03", meltk2$Hour, "CEST"))
ggplot(meltk2, aes(x=Hour , y = value, group = Hour, colour = variable)) +
geom_bar(stat = "identity") +
scale_x_datetime(breaks=date_breaks("1 hour"), labels=date_format("%H:%M:%S")) +
geom_text(aes(label = as.character(Label)), position = position_dodge(width = 0.8), vjust = -0.6)
最健康的方法是什么?
答案 0 :(得分:2)
您的意思是您希望每个时间戳只显示一个常见标签吗?将geom_text
的y值更改为数字会将所有3个标签设置为同一位置,实际上只显示一个:
ggplot(meltk2, aes(x=Hour, y = value, group = Hour, fill = variable)) +
geom_bar(stat = "identity") +
scale_x_datetime(breaks=date_breaks("1 hour"), labels=date_format("%H:%M:%S")) +
geom_text(aes(label = as.character(Label)), y=0, vjust = 1)
(我设置y = 0将标签置于底部。您可能想要选择其他高度。)
如果您不想堆叠酒吧(根据PoGibas&#39;回答):
ggplot(meltk2, aes(x=Hour, y = value, fill = variable)) +
geom_bar(stat = "identity", position="dodge") +
scale_x_datetime(breaks=date_breaks("1 hour"), labels=date_format("%H:%M:%S")) +
geom_text(aes(label = as.character(Label)), y=0, vjust = 1)