How to remove an error Discrete value in the chart - r

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.

Related

r ggplot not recognizing date format

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")

Trying to replicate a visualisation in R

Relatively inexperienced R user. I am trying to create something similar to the visualisation below with data for another country.
I've gone as far as creating the basic structure with data plotted in a vertical annual timeline with months running along the x axis but I have no idea how to edit the individual data points. I would appreciate any idea on how to move forward or even a completely different approach.
Here is my code using ggplot2:
p <- ggplot(forestfiresv, aes(y=year, x=dtstart))
p+geom_point() +
scale_x_datetime(lim=as.POSIXct(c("2021-01-01 00:01","2021-12-31 00:00", origin=lubridate::origin), "%m/%d %H:%M",tz="UTC"),expand = c(0,0), date_breaks="2 months", labels = date_format("%b"))+
theme_bw()
A data sample:
structure(list(year = c("2000", "2000", "2000", "2000", "2000",
"2000", "2000", "2000", "2000", "2000"), `Start date` = structure(c(11174, 11167, 11166, 11191,
11222, 11144, 11151, 11192, 11244, 11187), class = "Date"), `Start time` = c("02:15",
"16:05", "10:47", "15:41", "23:30", "15:29", "14:00", "13:53",
"17:39", "11:09"), `End date` = structure(c(11174,
11178, 11166, 11192, 11223, 11146, 11152, 11197, 11244, 11191
), class = "Date"), `End time` = c("14:00", "07:00", "19:00",
"22:00", "02:00", "12:00", "00:10", "13:30", "19:07", "11:30"
), Δάση = c(200, 1400, 400, 0, 0, 0, 600, 2000, 0, 260), `Forest` = c(800,
0, 0, 100, 100, 700, 0, 0, 0, 0), `Agricultural land` = c(0, 0, 0, 200, 0, 0, 200, 500, 0, 0), totalareaburnt = c(1000, 1400, 400, 500, 500, 700, 800, 2500, 350, 360), dtstart = structure(c(1628129700, 1627574700, 1627469220, 1629646860, 1632353400, 1625585340, 1626184800, 1629726780, 1634233140, 1629284940), class = c("POSIXct", "POSIXt"), tzone = "UTC"), dtend = structure(c(1628172000, 1628492400, 1627498800, 1629756000, 1632362400, 1625745600, 1626221400, 1630157400, 1634238420, 1629631800), class = c("POSIXct", "POSIXt"), tzone = "UTC")), .internal.selfref = <pointer: (nil)>, row.names = c(NA, 10L), class = c("data.table", "data.frame"))
This is the best I've obtained so far, but I bet it could be better. I've increased your example data frame because there was only one year of observation and I've injected some randomness to make the plot look better.
library(ggplot2)
ddf <- rbind(df,df,df,df,df,df,df,df,df,df)
ddf$year <- rep(2000:2009,each=10)
ddf$totalareaburnt <- sample(200:2500,100,replace = T)
ddf$dtstart <- ddf$dtstart+sample(86400*1:90,100,replace = T)
#duration in days
ddf$duration <- as.numeric(df$dtend-df$dtstart)/24
ddf$year <- as.integer(ddf$year)
ggplot(ddf,
aes(y = year,
x = dtstart)) +
geom_point(aes(size = totalareaburnt,
col = duration),
shape = 17,
alpha = 0.7) +
scale_x_datetime(
lim = as.POSIXct(
c("2021-01-01 00:01", "2021-12-31 00:00", origin = lubridate::origin),
"%m/%d %H:%M",
tz = "UTC"
),
expand = c(0, 0),
date_breaks = "1 months",
labels = scales::date_format("%b")
) +
theme_minimal() +
theme(
legend.position = "top",
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
axis.line = element_line(),
axis.ticks = element_line()
) +
scale_y_continuous(trans = "reverse", breaks = unique(ddf$year))+
scale_colour_gradientn(name= "Duartion (day)",colours = c( "yellow", "orange","darkred"))+
scale_size_continuous(name="Area burned (ha)")

Plot timelines without date

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()

Insert ticks for days with labels for weeks

In the following graph, I would like to insert ticks for days on x axis (date). There is an answer to x axis as continuous Adding minor tick marks to the x axis in ggplot2 (with no labels), but my x axis is date. As you can see, I have tried date_minor_breaks = "1 day" but unsuccessful. Any suggestions would be appreciated.
df<-structure(list(date = structure(c(1579132800, 1579219200, 1579305600,
1579392000, 1579478400, 1579564800, 1579651200, 1579737600, 1579824000,
1579910400, 1579996800, 1580083200, 1580169600, 1580256000, 1580342400,
1580428800, 1580515200, 1580601600, 1580688000, 1580774400, 1580860800,
1580947200, 1581033600, 1581120000, 1581206400, 1581292800, 1581379200,
1581465600, 1581552000, 1581638400, 1581724800, 1581811200, 1581897600,
1581984000, 1582070400, 1582156800, 1582243200, 1582329600, 1582416000),
class = c("POSIXct", "POSIXt"), tzone = "UTC"),cured = c(0, 0, 0, 0, 0, 0, 3, 3, 6, 7, 7, 13, 26, 34, 55,
77, 113, 180, 236, 372, 520, 723, 935, 1210, 1486, 1774,
2101, 2605, 2806, 3322, 3796, 4205, 4683, 5248, 5820, 6478,
7102, 7589, 7996)),
row.names = c(NA, -39L), class = "data.frame")
library(ggplot2)
df %>%
mutate(
x_date = as.Date(date)
) %>%
ggplot(aes(x = x_date, y = cured)) +
geom_point() +
scale_x_date(
date_breaks = "1 week",
date_minor_breaks = "1 day",
date_labels = "%d%b")
Created on 2020-02-24 by the reprex package (v0.3.0)
My work-around is to create a helper-data.frame, with breaks and (a lot of empty) labels. This way, you can use the major-breaks (with ticks!), and just label them with "".
df.labels <- df %>%
mutate( x_date = as.Date(date) ) %>%
#create labels on every Monday, using lubridate::wday, if not
#a Monday, add empty "" label.
mutate( label = ifelse( lubridate::wday( x_date ) == 2,
format( x_date, "%d%b" ),
"" ) )
df %>%
mutate(
x_date = as.Date(date)
) %>%
ggplot(aes(x = x_date, y = cured)) +
geom_point() +
scale_x_date(
breaks = df.labels$x_date,
labels = df.labels$label )

ggplot - adding vertical lines

Below is the data.table I am working with. I want to plot verticle lines whenever longSignal column is 1.
data.frame(
index = c("2011-09-09 17:00:00",
"2011-09-12 17:00:00",
"2011-09-13 17:00:00", "2011-09-14 17:00:00",
"2011-09-15 17:00:00", "2011-09-16 17:00:00", "2011-09-19 17:00:00",
"2011-09-20 17:00:00", "2011-09-21 17:00:00",
"2011-09-22 17:00:00", "2011-09-23 17:00:00", "2011-09-26 17:00:00",
"2011-09-27 17:00:00", "2011-09-28 17:00:00", "2011-09-29 17:00:00",
"2011-09-30 17:00:00", "2011-10-03 17:00:00",
"2011-10-04 17:00:00", "2011-10-05 17:00:00", "2011-10-06 17:00:00",
"2011-10-07 17:00:00"),
EURUSD.Close = c(1.36534, 1.367895, 1.36783, 1.37546, 1.38764, 1.38005,
1.36849, 1.37009, 1.35722, 1.346385, 1.35002, 1.353255,
1.35825, 1.35425, 1.359705, 1.33876, 1.31759, 1.33489, 1.33482,
1.34374, 1.33771),
longSignal = c(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0)
)
Here is the code that I was trying to work with in ggplot
ggplot(RSI_data, aes(index, EURUSD.Close)) +
geom_line() +
geom_vline(aes(xintercept = as.numeric(RSI_data$index[which(RSI_data$longSignal == 1)]),
size = 2, colour = "red"))
I have been getting error. Could anyone tell me how can I do this right? I
Thanks in advance!
You can try this:
# convert index to date-time format; this makes x-axis continuous rather than
# categorical, so you don't have to specify the group for geom_line.
RSI_data$index <- as.POSIXct(as.character(RSI_data$index))
ggplot(RSI_data,
aes(x = index, y = EURUSD.Close)) +
geom_line() +
geom_vline(data = subset(RSI_data, longSignal == 1), # filter data source
aes(xintercept = index),
size = 2, colour = "red")
If you put the xintercept term outside the aes() then it works. Also, you may get another error saying geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic? so the group=1 is there to stop that error
ggplot(RSI_data, aes(index,EURUSD.Close)) +
geom_line(group=1) +
geom_vline(xintercept = as.numeric(RSI_data$index[which(RSI_data$longSignal == 1)]),
size = 2, colour = "red")

Resources