ggplot2: geom_text 如何在条形图中绘制时间?
ggplot2: geom_text How to plot the time in the bars?
我有一张图表,上面标出了我入睡的时间和醒来的时间。
问题是图表中 geom_text 时间的绘图,我不能,我也不知道为什么。
这是 .csv csv
这是我的代码:
library(tidyverse)
prueba <- read.csv("C:/Users/......csv", fileEncoding = "UTF-8", header = TRUE, sep = ";")
### For read the , ###
prueba$airt = as.numeric(gsub(",","\.",prueba$airt))
prueba$airtmax = as.numeric(gsub(",","\.",prueba$airtmax))
#prueba$airtmax1 = as.Date(gsub(":":"\.",prueba$airtmax1))
#prueba$airtmax1 = as.character(gsub(",","\.",prueba$airtmax1))
prueba$airtmax1 <- as.numeric(prueba$airtmax1,
"%H-%M-%S")
我不知道如何阅读工作时间格式
ggplot(prueba, aes(x=date)) +
#scale_x_datetime(breaks = date_breaks("1 day")) +
scale_y_continuous(limits = c(-10,48), breaks=seq(-10,48,1),
labels=str_pad(seq(-10,48,1) %% 24, 2, pad="0")) +
##Lines which intercept the chart
geom_hline(yintercept=seq(0,48,24)) +
geom_text(data=prueba,aes(y=airtmax1,label=airtmax1),vjust=-1)+
如果你写成y=airtmax,label=airtmax
,效果很好
geom_linerange(aes(ymin = airt , ymax = airtmax ), color = "#63C300",size = 5) +
coord_flip() + ylab("Tiempo en horas") +
ggtitle("Horas de Sueño")
这是一个解决方案,它是将数据转换为正确的格式并确保 geom_text()
调用绘制的是正确的值。
prueba <-structure(list(date = c("21/05/20", "22/05/20", "23/05/20", "24/05/20",
"25/05/20", "26/05/20", "27/05/20", "28/05/20", "29/05/20", "30/05/20",
"31/05/20", "01/06/20", "02/06/20", "03/06/20", "04/06/20", "05/06/20",
"06/06/20", "07/06/20", "08/06/20", "09/6/20", "10/06/20", "11/06/20",
"12/06/20", "13/06/20"), airt1 = c("18:00:00", "01:00:00", "01:30:00",
"20:30:00", "00.00", "01:00:00", "01:00:00", "00.00", "01:00:00",
"01:00:00", "02:00:00", "01:00:00", "01:00:00", "01:15:00", "01:45:00",
"01:00:00", "01:15:00", "01:30:00", "02:00:00", "01:30:00", "20:30:00",
"01:00:00", "01:00:00", "02:00:00"), airtmax1 = c("08:00:00",
"06:00:00", "08:30:00", "07:00:00", "08:30:00", "09:15:00", "09:15:00",
"08:45:00", "09:00:00", "10:00:00", "09:30:00", "08:45:00", "08:03:00",
"08:30:00", "09:30:00", "09:23:00", "09:00:00", "09:30:00", "10:30:00",
"10:40:00", "11:00:00", "08:00:00", "10:45:00", "10:40:00"),
airt = c("18", "1", "1", "-2.5", "0", "1", "1", "0", "1",
"1", "2", "1", "1", "1,25", "1,7", "1", "1,25", "1,5", "2",
"1,5", "2,5", "1", "1", "2"), airtmax = c("32", "6", "5",
"7", "8", "9", "9", "8", "9", "10", "9,5", "8,75", "8,5",
"8,5", "9,5", "9,5", "9", "9,5", "10,5", "10,7", "11", "8",
"10,75", "10,75")), row.names = c(NA, -24L), class = "data.frame")
library(stringr)
#get data into proper format
prueba$airt = as.numeric(gsub(",","\.",prueba$airt))
prueba$airtmax = as.numeric(gsub(",","\.",prueba$airtmax))
#Convert to date time objects
prueba$airtmax1 <- as.POSIXct(prueba$airtmax1, "%H:%M:%S", tz="GMT")
prueba$date<-as.Date(prueba$date, "%d/%m/%y")
#verify data is in the correct format
str(prueba)
ggplot(prueba, aes(x=date)) +
scale_y_continuous(limits = c(-10,48), breaks=seq(-10,48,1),
labels=str_pad(seq(-10,48,1) %% 24, 2, pad="0")) +
##Lines wich intercept the chart
geom_hline(yintercept=seq(0,48,24)) +
#plot at end time and format the airtmax1 as hour:minute
geom_text(data=prueba, aes(y=airtmax, label=format(airtmax1, "%H:%M"), vjust=-1))+
geom_linerange(aes(ymin = airt , ymax = airtmax ), color = "#63C300",size = 5) +
coord_flip() + ylab("Tiempo en horas") +
ggtitle("Horas de Sueño")
注:此处粘贴图时缩放比例,过程中部分标签变形丢失
我有一张图表,上面标出了我入睡的时间和醒来的时间。
问题是图表中 geom_text 时间的绘图,我不能,我也不知道为什么。
这是 .csv csv
这是我的代码:
library(tidyverse)
prueba <- read.csv("C:/Users/......csv", fileEncoding = "UTF-8", header = TRUE, sep = ";")
### For read the , ###
prueba$airt = as.numeric(gsub(",","\.",prueba$airt))
prueba$airtmax = as.numeric(gsub(",","\.",prueba$airtmax))
#prueba$airtmax1 = as.Date(gsub(":":"\.",prueba$airtmax1))
#prueba$airtmax1 = as.character(gsub(",","\.",prueba$airtmax1))
prueba$airtmax1 <- as.numeric(prueba$airtmax1,
"%H-%M-%S")
我不知道如何阅读工作时间格式
ggplot(prueba, aes(x=date)) +
#scale_x_datetime(breaks = date_breaks("1 day")) +
scale_y_continuous(limits = c(-10,48), breaks=seq(-10,48,1),
labels=str_pad(seq(-10,48,1) %% 24, 2, pad="0")) +
##Lines which intercept the chart
geom_hline(yintercept=seq(0,48,24)) +
geom_text(data=prueba,aes(y=airtmax1,label=airtmax1),vjust=-1)+
如果你写成y=airtmax,label=airtmax
,效果很好
geom_linerange(aes(ymin = airt , ymax = airtmax ), color = "#63C300",size = 5) +
coord_flip() + ylab("Tiempo en horas") +
ggtitle("Horas de Sueño")
这是一个解决方案,它是将数据转换为正确的格式并确保 geom_text()
调用绘制的是正确的值。
prueba <-structure(list(date = c("21/05/20", "22/05/20", "23/05/20", "24/05/20",
"25/05/20", "26/05/20", "27/05/20", "28/05/20", "29/05/20", "30/05/20",
"31/05/20", "01/06/20", "02/06/20", "03/06/20", "04/06/20", "05/06/20",
"06/06/20", "07/06/20", "08/06/20", "09/6/20", "10/06/20", "11/06/20",
"12/06/20", "13/06/20"), airt1 = c("18:00:00", "01:00:00", "01:30:00",
"20:30:00", "00.00", "01:00:00", "01:00:00", "00.00", "01:00:00",
"01:00:00", "02:00:00", "01:00:00", "01:00:00", "01:15:00", "01:45:00",
"01:00:00", "01:15:00", "01:30:00", "02:00:00", "01:30:00", "20:30:00",
"01:00:00", "01:00:00", "02:00:00"), airtmax1 = c("08:00:00",
"06:00:00", "08:30:00", "07:00:00", "08:30:00", "09:15:00", "09:15:00",
"08:45:00", "09:00:00", "10:00:00", "09:30:00", "08:45:00", "08:03:00",
"08:30:00", "09:30:00", "09:23:00", "09:00:00", "09:30:00", "10:30:00",
"10:40:00", "11:00:00", "08:00:00", "10:45:00", "10:40:00"),
airt = c("18", "1", "1", "-2.5", "0", "1", "1", "0", "1",
"1", "2", "1", "1", "1,25", "1,7", "1", "1,25", "1,5", "2",
"1,5", "2,5", "1", "1", "2"), airtmax = c("32", "6", "5",
"7", "8", "9", "9", "8", "9", "10", "9,5", "8,75", "8,5",
"8,5", "9,5", "9,5", "9", "9,5", "10,5", "10,7", "11", "8",
"10,75", "10,75")), row.names = c(NA, -24L), class = "data.frame")
library(stringr)
#get data into proper format
prueba$airt = as.numeric(gsub(",","\.",prueba$airt))
prueba$airtmax = as.numeric(gsub(",","\.",prueba$airtmax))
#Convert to date time objects
prueba$airtmax1 <- as.POSIXct(prueba$airtmax1, "%H:%M:%S", tz="GMT")
prueba$date<-as.Date(prueba$date, "%d/%m/%y")
#verify data is in the correct format
str(prueba)
ggplot(prueba, aes(x=date)) +
scale_y_continuous(limits = c(-10,48), breaks=seq(-10,48,1),
labels=str_pad(seq(-10,48,1) %% 24, 2, pad="0")) +
##Lines wich intercept the chart
geom_hline(yintercept=seq(0,48,24)) +
#plot at end time and format the airtmax1 as hour:minute
geom_text(data=prueba, aes(y=airtmax, label=format(airtmax1, "%H:%M"), vjust=-1))+
geom_linerange(aes(ymin = airt , ymax = airtmax ), color = "#63C300",size = 5) +
coord_flip() + ylab("Tiempo en horas") +
ggtitle("Horas de Sueño")
注:此处粘贴图时缩放比例,过程中部分标签变形丢失