0

这个问题在这里可能已经有了答案。

我是 R 新手,我想使用 geom_line 绘制不同活动的每周时间安排。

我的数据结构是:

structure(list(time = structure(c(25L, 26L, 27L, 28L, 29L, 30L, 
31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 
44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 
57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 
70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 
83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 
96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 
107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 
118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 
129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 
140L, 141L, 142L, 143L, 144L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 
22L, 23L, 24L), .Label = c("00:00", "00:10", "00:20", "00:30", 
"00:40", "00:50", "01:00", "01:10", "01:20", "01:30", "01:40", 
"01:50", "02:00", "02:10", "02:20", "02:30", "02:40", "02:50", 
"03:00", "03:10", "03:20", "03:30", "03:40", "03:50", "04:00", 
"04:10", "04:20", "04:30", "04:40", "04:50", "05:00", "05:10", 
"05:20", "05:30", "05:40", "05:50", "06:00", "06:10", "06:20", 
"06:30", "06:40", "06:50", "07:00", "07:10", "07:20", "07:30", 
"07:40", "07:50", "08:00", "08:10", "08:20", "08:30", "08:40", 
"08:50", "09:00", "09:10", "09:20", "09:30", "09:40", "09:50", 
"10:00", "10:10", "10:20", "10:30", "10:40", "10:50", "11:00", 
"11:10", "11:20", "11:30", "11:40", "11:50", "12:00", "12:10", 
"12:20", "12:30", "12:40", "12:50", "13:00", "13:10", "13:20", 
"13:30", "13:40", "13:50", "14:00", "14:10", "14:20", "14:30", 
"14:40", "14:50", "15:00", "15:10", "15:20", "15:30", "15:40", 
"15:50", "16:00", "16:10", "16:20", "16:30", "16:40", "16:50", 
"17:00", "17:10", "17:20", "17:30", "17:40", "17:50", "18:00", 
"18:10", "18:20", "18:30", "18:40", "18:50", "19:00", "19:10", 
"19:20", "19:30", "19:40", "19:50", "20:00", "20:10", "20:20", 
"20:30", "20:40", "20:50", "21:00", "21:10", "21:20", "21:30", 
"21:40", "21:50", "22:00", "22:10", "22:20", "22:30", "22:40", 
"22:50", "23:00", "23:10", "23:20", "23:30", "23:40", "23:50"
), class = "factor"), variable = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Personalcare Mon-Fri", 
"Personalcare Saturday", "personalcareSun"), class = "factor"), 
    value = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 2, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 
    4, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 1, 2, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 0, 0, 
    0, 0, 0, 1, 1, 1, 0, 1, 1, 2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 
    1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c(NA, 
144L), class = "data.frame")

我想要一个线图和条形图

我需要如何转换我的数据?如何在 y 轴上表示百分比?

在我的代码下面,我尝试根据时间转换我的数据。

plot<-ggplot(Melted, aes(x = time, y = value, fill= variable)) + geom_col(position = "fill") 

 plot + labs(x="Time", y="Precentages", colour="Activties", fill="Activites")+theme(legend.position="right", axis.text.x = element_text(angle = 0, hjust = 0))+ annotate("rect", fill = "black", alpha = 0.3, xmin = c(73), xmax = c(97), ymin = -Inf, ymax = Inf) + scale_x_discrete(breaks = c( "04:00","05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00","13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00","22:00", "23:00", "00:00", "01:00", "02:00", "03:00"), expand = c(0,0)) + scale_y_continuous(expand = c(-0,0), labels = scales::percent) 

在此处输入图像描述

在此处输入图像描述

在我瞄准的图下方

下一个

4

1 回答 1

1

将时间转换为数字形式:

dat <- dat %>% 
  separate(time, c("hour", "minute"), remove=FALSE) %>% 
  mutate_at(vars(hour:minute), as.numeric) %>% 
  mutate(time_new = hour + minute/60)


ggplot(dat, aes(time_new, value)) + 
  geom_line() + 
  scale_x_continuous("Time", breaks=seq(0,24,3), labels=sprintf("%02d:00", seq(0,24,3)))

在此处输入图像描述

将需要数据集中的其他因素来弄清楚如何回答您关于百分比计算的其他查询。

于 2019-08-08T06:38:39.470 回答