Related
I have the following chart.
p1 <- ggplot(data = mydat, aes(x = time))+
geom_line(aes(y = sumabsdiff, colour = 'sumabsdiff'))+
geom_line(aes(y = windsize, col='windsize'))+
scale_x_time(breaks = scales::date_breaks('1 sec'))+ #('15 secs'))+
scale_color_manual(values=c('sumabsdiff' = 'black',
"windsize" = "red"))+
theme(legend.position = "top")
As you can see, the date is all messed up even though time seems perfectly fine to me.
> mydat$time
[1] "2022-09-19 12:44:47 UTC" "2022-09-19 12:44:48 UTC" "2022-09-19 12:44:49 UTC" "2022-09-19 12:44:50 UTC"
[5] "2022-09-19 12:44:50 UTC" "2022-09-19 12:44:50 UTC".
Any idea why?
Data:
mydf <- structure(list(time = structure(c(1663591487.801, 1663591488.614,
1663591489.626, 1663591490.097, 1663591490.202, 1663591490.717
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), bid = c(11735.68,
11735.18, 11734.93, 11734.43, 11734.3, 11734.43), ask = c(11737.58,
11737.08, 11736.83, 11736.33, 11736.2, 11736.33), flags = c(6,
6, 6, 6, 6, 6), typical = c(11736.63, 11736.13, 11735.88, 11735.38,
11735.25, 11735.38), row = 266:271, prevrow_short = c(258L, 258L,
260L, 261L, 262L, 265L), windsize = c(9, 10, 9, 9, 9, 7), diff = c(-0.119999999998981,
-0.5, -0.25, -0.5, -0.130000000001019, 0.130000000001019), absdiff = c(0.119999999998981,
0.5, 0.25, 0.5, 0.130000000001019, 0.130000000001019), sumabsdiff = c(3.60999999999694,
4.10999999999694, 3.72999999999593, 3.85999999999694, 3.61999999999898,
2.13000000000102), positive = c(FALSE, FALSE, FALSE, FALSE, FALSE,
TRUE), meanpos = c(0.444444444444444, 0.4, 0.333333333333333,
0.222222222222222, 0.222222222222222, 0.285714285714286), posdiff = c(0,
0, 0, 0, 0, 0.130000000001019), negdiff = c(0.119999999998981,
0.5, 0.25, 0.5, 0.130000000001019, 0), sumposdiff_short = c(1.36999999999898,
1.36999999999898, 1.23999999999796, 0.869999999998981, 0.869999999998981,
0.630000000001019), sumnegdiff_short = c(2.23999999999796, 2.73999999999796,
2.48999999999796, 2.98999999999796, 2.75, 1.5), power_short = c(0.37950138504159,
0.333333333333333, 0.332439678283999, 0.225388601036184, 0.240331491712493,
0.295774647887661), market_open = c(FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE), timediff = c(0.219000101089478, 0.812999963760376,
1.01199984550476, 0.470999956130981, 0.105000019073486, 0.515000104904175
), avgspeed = c(2.71247733209185, 2.42072135038066, 2.44698207323042,
2.3255814641193, 2.8019926211971, 2.14658085742225), relative_positive_diff = c(0.37950138504159,
0.333333333333333, 0.332439678283999, 0.225388601036184, 0.240331491712493,
0.295774647887661), timesec = structure(c(1663591487, 1663591488,
1663591489, 1663591490, 1663591490, 1663591490), class = c("POSIXct",
"POSIXt"), tzone = "UTC")), pandas.index = <environment>, row.names = 266:271, class = "data.frame")
By the way, the time actually includes milliseconds, perhaps that is the cause
?scale_x_time:
These are the default scales for the three date/time class. These
will usually be added automatically. To override manually, use
scale_*_date for dates (class 'Date'), scale_*_datetime for
datetimes (class 'POSIXct'), and scale_*_time for times (class
'hms').
Your time variable is class POSIXt, not hms, so you should be using scale_x_datetime instead.
ggplot(data = mydf, aes(x = time))+
geom_line(aes(y = sumabsdiff, colour = 'sumabsdiff'))+
geom_line(aes(y = windsize, col='windsize'))+
scale_x_datetime(breaks = "1 secs") +
scale_color_manual(values=c('sumabsdiff' = 'black',
"windsize" = "red"))+
theme(legend.position = "top")
You can format the axis labels with date_labels= and %-codes (listed in ?strptime):
ggplot(data = mydf, aes(x = time))+
geom_line(aes(y = sumabsdiff, colour = 'sumabsdiff'))+
geom_line(aes(y = windsize, col='windsize'))+
# scale_x_datetime(breaks = scales::date_breaks('1 sec'))+ #('15 secs'))+
scale_x_datetime(breaks = "1 sec", date_labels = "%H:%M:%S") +
scale_color_manual(values=c('sumabsdiff' = 'black',
"windsize" = "red"))+
theme(legend.position = "top")
This question already has answers here:
ggplot2 and date on x-axis
(1 answer)
Problems with changing the date scale on an axis - ggplot
(1 answer)
Closed 7 months ago.
I wish to plot my data by using the date on the x axis and the number of project launched on the y axis
I am currently using this code
plot1 <- ggplot()+
geom_line(data=data0,mapping = aes(x = date, y = launches, group=1) ) +
geom_line(data=data0,mapping = aes(x = date, y = US, group=1), colour="blue" )+
ggtitle("Kickstarter")
However, i realised that i have so many dates that the Y axis becomes difficult to read
Instead i would wish for the Y axis to just show the months
Is there any way to do this ?
structure(list(date = c("2021-01-01", "2021-01-02", "2021-01-03",
"2021-01-04", "2021-01-05", "2021-01-06"), launches = c(4, 0,
0, 0, 8, 4), pledged = c(50278.64, 0, 0, 0, 366279.590415302,
172073.0471292), backers = c(2880, 0, 0, 0, 6588, 3528), total_goal = c(24000,
0, 0, 0, 148000, 60000), mean_goal = c(6000, 0, 0, 0, 18500,
15000), US = c(4, 0, 0, 0, 4, 0), `number of success` = c(4,
0, 0, 0, 8, 4), duration_days = c(30, 0, 0, 0, 31, 30), Twitter = c(1324L,
1548L, 1297L, 1585L, 1636L, 1583L), replies = c(882L, 1252L,
910L, 1018L, 810L, 1000L), likes = c(22859L, 24375L, 17854L,
20341L, 19521L, 19401L), retweets = c(8621L, 8239L, 6141L, 6728L,
6938L, 6842L)), row.names = c(NA, 6L), class = "data.frame")
edit: my apologies, i inversed teh x and y axis in my explanation
Assume you meant date on x axis
library(tidyverse)
library(lubridate)
data0$date<-ymd(data0$date)
data0$month<-months(data0$date)
plot1 <- ggplot()+
geom_line(data=data0,mapping = aes(x = date, y = launches, group=1) ) +
geom_line(data=data0,mapping = aes(x = date, y = US, group=1), colour="blue" )+
ggtitle("Kickstarter")
I changed your 'example' dataset to better illustrate a potential solution:
library(ggplot2)
data0 <- structure(list(date = c("2021-01-01", "2021-01-15", "2021-02-01",
"2021-02-15", "2021-03-01", "2021-03-15"), launches = c(4, 0,
0, 0, 8, 4), pledged = c(50278.64, 0, 0, 0, 366279.590415302,
172073.0471292), backers = c(2880, 0, 0, 0, 6588, 3528), total_goal = c(24000,
0, 0, 0, 148000, 60000), mean_goal = c(6000, 0, 0, 0, 18500,
15000), US = c(4, 0, 0, 0, 4, 0), `number of success` = c(4,
0, 0, 0, 8, 4), duration_days = c(30, 0, 0, 0, 31, 30), Twitter = c(1324L,
1548L, 1297L, 1585L, 1636L, 1583L), replies = c(882L, 1252L,
910L, 1018L, 810L, 1000L), likes = c(22859L, 24375L, 17854L,
20341L, 19521L, 19401L), retweets = c(8621L, 8239L, 6141L, 6728L,
6938L, 6842L)), row.names = c(NA, 6L), class = "data.frame")
plot1 <- ggplot(data0) +
geom_line(aes(x = date, y = launches, group = 1) ) +
geom_line(aes(x = date, y = US, group = 1), colour="blue") +
ggtitle("Kickstarter")
plot1
# Change the format from "character" to "date"
data0$date <- as.Date(data0$date)
# Then you can change the breaks on the x axis
plot2 <- ggplot(data0) +
geom_line(aes(x = date, y = launches, group = 1) ) +
geom_line(aes(x = date, y = US, group = 1), colour="blue") +
ggtitle("Kickstarter") +
scale_x_date(date_breaks = "1 month")
plot2
Created on 2022-07-27 by the reprex package (v2.0.1)
Does this solve your problem?
I have a carbon dioxide sensor that captures the concentration during working time.
Now I'd like to create a line plot that shows one line for each working day over time (from 8 am to 6 pm).
Some sample data:
co2data <- data.frame(
dateTime = c(
"2021-08-18 08:00:00",
"2021-08-18 09:00:00",
"2021-08-18 10:00:00",
"2021-08-18 11:00:00",
"2021-08-18 12:00:00",
"2021-08-18 13:00:00",
"2021-08-18 14:00:00",
"2021-08-18 15:00:00",
"2021-08-18 16:00:00",
"2021-08-18 17:00:00",
"2021-08-18 18:00:00",
"2021-08-19 08:00:00",
"2021-08-19 09:00:00",
"2021-08-19 10:00:00",
"2021-08-19 11:00:00",
"2021-08-19 12:00:00",
"2021-08-19 13:00:00",
"2021-08-19 14:00:00",
"2021-08-19 15:00:00",
"2021-08-19 16:00:00",
"2021-08-19 17:00:00",
"2021-08-19 18:00:00"
),
ppm = c(
400,
450,
456,
560,
670,
690,
810,
900,
600,
650,
700,
410,
470,
480,
590,
700,
710,
810,
900,
1010,
1000,
1100
)
)
Now I can plot the concentration over time, but I don't know how to plot times only on the x-axis (no dates).
co2data <- co2data %>% mutate(dateTime = as.POSIXct(dateTime))
co2data %>%
ggplot(aes(x = dateTime, y = ppm)) +
geom_line() +
labs(title = "CO2-Concentration", y = "CO2-concentration ppm", x = "Time")
How can I plot one line for each day?
With the help of the data.table package (or lubridate) you can extract temporal information form Date/Time strings.
require(data.table)
require(ggplot2)
setDT(co2data)
co2data[ , time := hour(as.ITime(dateTime)) ]
co2data[ , yday := as.factor(yday(as.IDate(dateTime))) ]
ggplot(co2data, aes(x = time, y = ppm, col = yday)) +
geom_line() +
labs(title = "CO2-Concentration", y = "CO2-concentration ppm", x = "Time") +
theme_bw()
My code:
df1 <- data.frame(
time = as.POSIXct(c("2021-05-04 01:00:00", "2021-05-04 02:00:00",
"2021-05-04 03:00:00", "2021-05-04 04:00:00", "2021-05-04 05:00:00",
"2021-05-04 06:00:00", "2021-05-04 07:00:00", "2021-05-04 08:00:00")),
lng = c(15.363, 15.363, 15.363, 15.363 , 15.363, 15.363, 15.363, 15.363),
lat = c(51.232, 51.232, 51.232, 51.232, 51.232, 51.232, 51.232, 51.232),
name = c('aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg', 'hhh'),
intensity = c(0, 7, 20, 200, 500, 500, 600, 800))
przedzialy <- seq(0, 1200, length.out=9)
df1_new <- df1 %>%
mutate(zakresy = cut(intensity, breaks = przedzialy))
paleta_rgb <- c("#41786E", "#5AA03C", "#BED200", "#FFFA78", "#FCDC00",
"#F5BE00", "#F0A04B", "#E68246", "#E15F32", "#D2412D")
# intensity
ggplot() +
geom_point(data = df1_new, aes(lng, lat, color = zakresy), alpha = 0.5, show.legend = TRUE, size = 2) +
scale_color_gradientn(colors = paleta_rgb) +
coord_map() +
theme_void()
I am getting this error:
Discrete value supplied to continuous scale
Additionally, I don't know how to set the range for intensity to 0, currently I have NA and I would like to get ranges for the scale:
equal to 0
(0; 100>
(100; 200>
...
(1100; 1200>
Thanks in advance for your help.
I have a question about line colours in ggplot2. I need to plot solar radiation data but I only have 6 hourly data, so geom_line doest not give a "nice" outuput. I've tried geom_smooth and the result is close to what I need. But I have a new question, is it possible to change line colour depending on the y value?
The code used for the plot is
library(ggplot2)
library(lubridate)
# Lectura de datos
datos.uvi=read.csv("serie-temporal-1.dat",sep=",",header=T,na.strings="-99.9")
datos.uvi=within(datos.uvi, fecha <- ymd_h(datos.uvi$fecha.hora))
# geom_smooth
ggplot(data=datos.uvi, aes(x=fecha, y=Rad_Global_.mW.m2., colour="GLOBAL")) +
geom_smooth(se=FALSE, span=0.3)
In the desired output, line should be red for radiation values under 250, green in the 250-500 interval and blue for values higher than 500.
Is it possible with geom_smooth? I've tried to reuse code here, but could not find the point.
Data used for the plot:
dput(datos.uvi)
structure(list(fecha.hora = c(2016012706L, 2016012712L, 2016012718L,
2016012800L, 2016012806L, 2016012812L, 2016012818L, 2016012900L,
2016012906L, 2016012912L, 2016012908L, 2016013000L), latitud = c(37.75,
37.75, 37.75, 37.75, 37.75, 37.75, 37.75, 37.75, 37.75, 37.75,
37.75, 37.75), longitud = c(-1.25, -1.25, -1.25, -1.25, -1.25,
-1.25, -1.25, -1.25, -1.25, -1.25, -1.25, -1.25), altitud = c(300L,
300L, 300L, 300L, 300L, 300L, 300L, 300L, 300L, 300L, 300L, 300L
), cobertura_nubosa = c(0.91, 0.02, 0.62, 1, 0.53, 0.49, 0.01,
0, 0, 0.13, 0.62, 0.84), longitud_de_onda_inicial.nm. = c(284.55,
284.55, 284.55, 284.55, 284.55, 284.55, 284.55, 284.55, 284.55,
284.55, 284.55, 284.55), Rad_Global_.mW.m2. = c(5e-04, 259.2588,
5, 100.5, 1, 886.5742, 110, 40, 20, 331.3857, 0, 0), Rad_Directa_.mW.m2. = c(0,
16.58034, 0, 0, 0, 202.5683, 0, 0, 0, 89.81712, 0, 0), Rad_Difusa_.mW.m2. = c(0,
242.6785, 0, 0, 0, 684.0059, 0, 0, 0, 241.5686, 0, 0), Angulo_zenital_.º. = c(180,
56.681, 180, 180, 180, 56.431, 180, 180, 180, 56.176, 180, 180
), blank = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
fecha = structure(c(1453874400, 1453896000, 1453917600, 1453939200,
1453960800, 1453982400, 1454004000, 1454025600, 1454047200,
1454068800, 1454054400, 1454112000), tzone = "UTC", class = c("POSIXct",
"POSIXt"))), row.names = c(NA, -12L), .Names = c("fecha.hora",
"latitud", "longitud", "altitud", "cobertura_nubosa", "longitud_de_onda_inicial.nm.",
"Rad_Global_.mW.m2.", "Rad_Directa_.mW.m2.", "Rad_Difusa_.mW.m2.",
"Angulo_zenital_.º.", "blank", "fecha"), class = "data.frame")
Thanks in advance.
Calculate the smoothing outside ggplot2 and then use geom_segment:
fit <- loess(Rad_Global_.mW.m2. ~ as.numeric(fecha), data = datos.uvi, span = 0.3)
#note the warnings
new.x <- seq(from = min(datos.uvi$fecha),
to = max(datos.uvi$fecha),
by = "5 min")
new.y <- predict(fit, newdata = data.frame(fecha = as.numeric(new.x)))
DF <- data.frame(x1 = head(new.x, -1), x2 = tail(new.x, -1) ,
y1 = head(new.y, -1), y2 = tail(new.y, -1))
DF$col <- cut(DF$y1, c(-Inf, 250, 500, Inf))
ggplot(data=DF, aes(x=x1, y=y1, xend = x2, yend = y2, colour=col)) +
geom_segment(size = 2)
Note what happens at the cut points. If might be more visually appealing to make the x-grid for prediction very fine and then use geom_point instead. However, plotting will be slow then.
This is not really what you asked for, but might serve the same purpose: instead of colouring the line, colour the background. First we create a dataframe of rectangle/limit coordinates.
rect_data <- data.frame(xmin=min(datos.uvi$fecha),
xmax=max(datos.uvi$fecha),
ymin=c(0,250,500),
ymax=c(250,500,max(datos.uvi$Rad_Global_.mW.m2.)),
col=c("red","green","blue"))
Then we add them to the plot, using scale_fill_identity()
ggplot(data=datos.uvi) +
geom_smooth(aes(x=fecha, y=Rad_Global_.mW.m2.),colour="black",se=FALSE, span=0.3) +
geom_rect(data=rect_data, aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax,fill=col),alpha=0.1)+
scale_fill_identity()