R 在ggplot中将原点设置为从0开始

R 在ggplot中将原点设置为从0开始,r,ggplot2,R,Ggplot2,如何在ggplot2中设置y轴和x轴的原点/截取 我有如下数据集(问题末尾的数据结构): 我正在使用以下代码绘制数据 plot <- ggplot(melted_Activities, aes(x = Var1,y = value,fill = Var2)) + geom_col(position = "fill") + labs(x="Time", y="Probabilities", c

如何在ggplot2中设置y轴和x轴的原点/截取

我有如下数据集(问题末尾的数据结构):

我正在使用以下代码绘制数据

plot <- ggplot(melted_Activities, aes(x = Var1,y = value,fill = Var2)) +
         geom_col(position = "fill")  + 
         labs(x="Time", y="Probabilities", 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)

数据:
melled_活动查看数据和说明,从午夜到4点之前的几个小时似乎是从第二天开始的。这使得字符到日期时间格式的转换稍微复杂一些。这就是
dplyr
管道中的
if_else
背后的原因。确保使用的是
dplyr::if_else
,因为
base::ifelse
会弄乱时间格式

注释中
现在需要插入希望“阴影框”出现的时间,而不是任意数字

他们说,
扩大了评论工作中建议的限制。请注意,
R
在将纯时间字符串转换为datetime时,默认情况下会考虑当前的
Sys.Date()
。这就是为什么在
expand\u limits
中,我使用数据帧本身转换的日期时间来解释这一点

library(lubridate)
library(dplyr)
library(ggplot2)

melted_ActivitiesC <- melted_Activities %>% 
                       mutate(time = as.POSIXct(strptime(Var1, format="%H:%M"))) %>% 
                       mutate(time = if_else(hour(time) >= 4, time, 
                                                              time + as.difftime(1, unit="days")))



ggplot(melted_Activities, aes(x = time,y = value,fill = Var2)) +
  geom_col(position = "fill")  + 
  labs(x="Time", y="Probabilities", 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 = as.POSIXct(strptime("16:00", format="%H:%M")), 
           xmax = as.POSIXct(strptime("24:00", format="%H:%M")), 
           ymin = -Inf, ymax = Inf) + scale_x_datetime(date_labels = "%H") + 
           expand_limits(x =floor_date(min(melted_Activities$time), "day"), y = 0)
库(lubridate)
图书馆(dplyr)
图书馆(GG2)
熔化的_活动c%
mutate(time=as.POSIXct(strtime(Var1,format=“%H:%M”)))%>%
变异(时间=if_else(小时(时间)>=4,时间,
时间+as.difftime(1,unit=“days”))
ggplot(熔化的放射性,aes(x=时间,y=值,填充=Var2))+
几何坐标(position=“fill”)+
实验室(x=“时间”,y=“概率”,color=“活动”,fill=“活动”)+
主题(legend.position=“right”,axis.text.x=元素\文本(角度=0,hjust=0))+
注释(“rect”,fill=“black”,alpha=0.3,
xmin=as.POSIXct(strtime(“16:00”,format=“%H:%M”),
xmax=as.POSIXct(strtime(“24:00”,format=“%H:%M”),
ymin=-Inf,ymax=Inf)+scale_x_datetime(date_labels=“%H”)+
扩展限制(x=最低日期(最小值(活动$time),“天”),y=0)

扩展
不是一个限制,它是您将扩展范围的数量。您需要
limits
参数。如果@camile可能重复,请尝试解决方案。最后,它可以工作,但需要在该解决方案生效之前采取其他步骤。@M-M这是公平的,我没有意识到时间被编码为字符。如果您将时间转换为实际的datetime对象,这将更容易,并且您可以根据需要对其进行格式化。有很多帖子也涉及到这一点。问题的关键是,在量表中设置了错误的参数。@camille转换过程中也有一点复杂。总而言之,我对结束这个问题犹豫不决。干杯。@user10974052不客气。旁白。午夜到4点之间的时间点是从第二天开始的,如果你在你的问题中把它弄清楚就更好了。对于试图回答你问题的人来说,这可能并不明显。最好把所有的信息都列出来。干杯。以下是为什么(?)
ifelse
会这样做:
plot + 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))
melted_Activities <- structure(list(Var1 = structure(1:5, .Label = c("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", "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"), class = "factor"), Var2 = structure(c(1L, 
1L, 1L, 1L, 1L), .Label = c("Personal care", "Sleep", "Eating", 
"Other personal care", "Employment", "Main job", "Second job", 
"Activites related to employment", "Study", "School or University", 
"Free time study", "House and family care", "Food management", 
"Household maintenance", "Making care for textiles", "Gardening and pet care", 
"Construction and repairs", "Shopping and services", "Household management", 
"Childcare of own household member", "Help to an adult household member", 
"Volunteer work and meeting", "Organisational work", "Informal help to others", 
"Participatory activties", "Social care", "Social life", "Entertainment and culture", 
"Resting", "Sport and outdoor activities", "Physical exercise", 
"Productive exercise", "Sportsrelated activities", "Hobbies, games and computing", 
"Arts and hobbies", "Computing", "PC Games", "Mass media", "Reading", 
"Tv and video", "Radio and music", "Travel and unspecified time use", 
"Travel by purpose", "Punctuating activity"), class = "factor"), 
 value = c(2, 2, 2, 2, 2)), row.names = c(NA, 5L), class = "data.frame")
library(lubridate)
library(dplyr)
library(ggplot2)

melted_ActivitiesC <- melted_Activities %>% 
                       mutate(time = as.POSIXct(strptime(Var1, format="%H:%M"))) %>% 
                       mutate(time = if_else(hour(time) >= 4, time, 
                                                              time + as.difftime(1, unit="days")))



ggplot(melted_Activities, aes(x = time,y = value,fill = Var2)) +
  geom_col(position = "fill")  + 
  labs(x="Time", y="Probabilities", 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 = as.POSIXct(strptime("16:00", format="%H:%M")), 
           xmax = as.POSIXct(strptime("24:00", format="%H:%M")), 
           ymin = -Inf, ymax = Inf) + scale_x_datetime(date_labels = "%H") + 
           expand_limits(x =floor_date(min(melted_Activities$time), "day"), y = 0)