My df:
prod
# A tibble: 695 × 3
REPORT_DATE UNIT PROD
<date> <chr> <dbl>
1 2015-03-28 DEP11 2.043962
2 2015-03-29 DEP11 2.788490
3 2015-03-30 DEP11 2.795274
4 2015-03-31 DEP11 3.100589
5 2015-04-01 DEP11 2.882843
6 2015-04-02 DEP11 2.987861
7 2015-04-03 DEP11 3.123047
8 2015-04-04 DEP11 3.264180
9 2015-04-05 DEP11 2.987729
10 2015-04-06 DEP11 3.222573
# ... with 685 more rows
I created a ggTimeSeries plot as below:
I want to change the colour scheme...and want to divide the colour into 3 categories:
below 3.0 = red
3.0 - 3.2 = amber
greater than 3.2 = green
I have tried the following:
ggplot_calendar_heatmap(
prod,
'REPORT_DATE',
'PROD'
) +
xlab('') +
ylab('') +
scale_fill_continuous(low = 'red', high = 'green') +
facet_wrap(~Year, ncol = 1)
also tried to use scale_colour_gradientn and scale_colour_manuel but no luck... any ideas?
Something like this should work:
set.seed(1)
# generate some random data
prod <- data.frame(REPORT_DATE=seq.Date(as.Date('2015/01/03'), as.Date('2017/02/28'), by='day'))
prod$PROD <- runif(nrow(prod), 0, 5)
prod <- transform(prod, PROD.cut=cut(PROD, breaks=c(-Inf,3, 3.2,Inf))) # bin data
library(ggTimeSeries)
ggplot_calendar_heatmap(
prod,
'REPORT_DATE',
'PROD.cut'
) +
xlab('') +
ylab('') +
scale_fill_manual(values = c("red", "orange", "green")) +
#scale_fill_continuous(low = 'red', high = 'green') +
facet_wrap(~Year, ncol = 1)
Related
I have a facet plot that I need to place a rectangle in or highlight 3 specific facets. Facets 5, 6, and 10. See Below:
I found some code referring to "geom_rect" that seems like it may work but it won't show up, also doesn't give me any error message. Here is the code:
weekly_TS_PDF<- ggplot(TS_stack, aes(x= TS_log, y = TS_depth, color= sentiment)) +
scale_y_reverse(limits= c(16,2), breaks= seq(16,2)) +
geom_rect(data = data.frame(Week = 5), aes(xmin = -65, xmax = -55, ymin = 1, ymax = 16), alpha = .3, fill="grey", inherit.aes = F) +
geom_point() + facet_grid(.~ Week) + geom_hline(data = week_avg_15E, aes(yintercept = x), linetype = "solid") +
ylab("Target Depth (m)") + xlab("Mean Target Strength (dB)") + ggtitle("Mean TS by Depth by Week (12 hour resolution)") +
guides(color=guide_legend("Year"))
Reprex data:
X TS_depth Group.1 x TS_log Date_time AMPM Week sentiment
1 1 9.593093 2020-12-01 18:00:00 5.390264e-07 -62.68390 2020-12-01 18:00:00 PM 5 Year 1
2 2 9.550032 2020-12-02 06:00:00 4.022841e-07 -63.95467 2020-12-02 06:00:00 AM 6 Year 1
3 3 9.677069 2020-12-02 18:00:00 6.277191e-07 -62.02235 2020-12-02 18:00:00 PM 7 Year 1
4 4 9.679256 2020-12-03 06:00:00 3.501608e-07 -64.55732 2020-12-03 06:00:00 AM 8 Year 1
5 5 9.606380 2020-12-03 18:00:00 6.698625e-07 -61.74014 2020-12-03 18:00:00 PM 9 Year 1
6 6 9.548408 2020-12-04 06:00:00 4.464622e-07 -63.50215 2020-12-04 06:00:00 AM 10 Year 1
I just need to highlight or put a rectangle in facets 5,6, and 10. Any help is appreciated.
I'm making plots behavior changes overtime. I would like to include shaded areas to emphasize some phases. Currently I'm adding shaded areas using annotate("rect" x=POSIXct("")..and then adding annotate("label")...
Is there a way to add the label directly to the shaded area, where the size and wrapping of the label text changes to fit within the shaded area? Or, is there a more efficient way to try to position a label in a date range shaded area?
myDF <- labeled_rectxlsx
> str(myDF)
tibble [37 × 3] (S3: tbl_df/tbl/data.frame)
$ schDate : POSIXct[1:37], format: "2020-10-22" "2020-10-23" "2020-11-09"
"2020-11-12" ...
$ rating : num [1:37] NA NA NA NA NA NA NA NA NA NA ...
$ rect_label: chr [1:37] NA NA NA NA ...
schDate rating rect_label
...
11 2020-12-14 NA <NA>
12 2020-12-15 NA <NA>
13 2020-12-16 NA <NA>
14 2020-12-17 NA <NA>
15 2020-12-18 NA Winter Break
16 2021-01-03 NA <NA>
17 2021-01-04 6 <NA>
18 2021-01-05 8 <NA>
19 2021-01-06 5 <NA>
20 2021-01-07 8 <NA>
21 2021-01-08 7 <NA>
22 2021-01-11 6 <NA>
maxY <- max(labeled_rectxlsx$rating, na.rm=TRUE)
insideY= (maxY*.98)
p <- myDF %>%
ggplot(aes(x= schDate, y=rating))+
geom_point()+
geom_line()+
theme_classic()+
theme(
plot.title = element_text(size=20, face="bold"),
axis.title.x = element_blank(),
axis.title.y = element_text(size=14, face="bold"),
axis.text.x = element_text(size=12),
axis.text.y = element_text(size=12))+
annotate("rect",
xmin = as.POSIXct("2020-12-19"), xmax = as.POSIXct("2021-01-03"),
ymin = -Inf, ymax = Inf, fill = "gray", alpha =.5)+
annotate("label", x=as.POSIXct("2020-12-27"),
y= insideY , label="Winter Break", fill="black", color="white",fontface="bold",
size=4, vjust="inward")
p
To expand on the comment a bit, you could use the ggfittext package with a dummy data.frame to hold the values, in combination with inherit.aes = FALSE.
Example below:
library(ggfittext)
#> Warning: package 'ggfittext' was built under R version 4.0.3
library(ggplot2)
set.seed(0)
myDF <- data.frame(
schDate = seq(Sys.Date() - 100, Sys.Date(), by = "1 day"),
rating = cumsum(rnorm(101))
)
ggplot(myDF, aes(schDate, rating)) +
geom_point() +
geom_line() +
annotate("rect",
xmin = as.Date("2020-12-19"), xmax = as.Date("2021-01-03"),
ymin = -Inf, ymax = Inf, fill = "gray", alpha =.5) +
geom_fit_text(
data = data.frame(xmin = as.Date("2020-12-19"), xmax = as.Date("2021-01-03"),
label = "Winter Break"),
aes(xmin = xmin, xmax = xmax, ymin = 4, ymax = 5, label = label),
inherit.aes = FALSE
)
Created on 2021-02-09 by the reprex package (v1.0.0)
I am attempting to plot sea level pressure (isobars) contour on an spatial area with 2mb spacing and want a specific isobar (1015) plotted as thicker line than the others and wonder if it is possible with other function and/or with ggplot/geom_conour related function.
Below the command I sued to plot the isobar. Now any idea on how to add a oommand for the 1015 isobar?
syn_plot <- ggplot() +
geom_tile(data = synclas_gather_df, aes(x=x, y=y, fill=value)) +
geom_sf(data = map_bg, fill="transparent")+
geom_contour2(data = synclas_gather_df, aes(x=x,y=y,z=value), binwidth = 2, color = "black") +
scale_fill_gradientn(colours = colorRamps::matlab.like2(100), name = "hPa",breaks=0:5) +
scale_colour_gradient(guide = 'none') + facet_wrap(~key, ncol = 4) +
scale_x_continuous(limits = c(-34,29), expand = c(0, 0))+
scale_y_continuous(limits = c(-9,34), expand = c(0,0))+
theme_bw() + theme(axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank())
syn_plot + geom_text_contour(data= synclas_gather_df,aes(x,y,z = value), stroke = 0.10,binwidth = 4,size=3)
data format:
time lon lat slp
1 1978-12-30 12:00:00 0 40 1015.
2 1978-12-30 12:00:00 2.5 40 1013.
3 1978-12-30 12:00:00 5 40 1012.
4 1978-12-30 12:00:00 7.5 40 1010.
5 1978-12-30 12:00:00 10 40 1007.
6 1978-12-30 12:00:00 12.5 40 1005.
7 1978-12-30 12:00:00 15 40 1004.
8 1978-12-30 12:00:00 17.5 40 1003.
9 1978-12-30 12:00:00 20 40 1002.
10 1978-12-30 12:00:00 22.5 40 1001.
I have to plot the same graph a couple of times with different rectangles.
head(df)
DATA n
<date> <int>
1 2018-01-02 243
2 2018-01-03 243
3 2018-01-04 221
4 2018-01-05 211
5 2018-01-06 35
6 2018-01-07 30
head(rectangles)
channel begin end
<chr> <date> <date>
1 aaaaaaaaaaaaa 2018-09-28 2018-12-28
2 bbbb 2018-08-31 2018-10-31
3 cccccccccccccc 2018-08-31 2018-10-31
4 aaaaaaaaaaaaaaaaaaaaaaa 2018-08-31 2018-10-31
5 ddddddddddddddddddddddddddddddd 2018-08-31 2018-10-31
What I have done so far to have many plots with the same data of df but with the unique rectangles$channels:
unique_rectangles <- unique(rectangles$channel)
for (rect in unique_rectangles) {
plot <- ggplot(df, aes(x = DATA, y =n)) +
geom_rect(data = subset(rectangles, rectangles$channel==unique_rectangles[ret]), aes(xmin = begin, xmax = end, ymin = -Inf, ymax = +Inf), inherit.aes = FALSE, fill = 'red', alpha = 0.2) +
geom_line() +
ggtitle(paste(unique_rectangles[ret]))
print(plot)}
But all I got is:
Error: Aesthetics must be either length 1 or the same as the data (1): xmin, xmax
What can I do to have the multiples plots?
I have a data frame that looks like this
head(data1, 30)
date cur.gas
1 2015-01-01 00:00:45 RD
2 2015-01-01 00:02:45 RD
3 2015-01-01 00:04:45 RD
4 2015-01-01 00:06:45 RD
5 2015-01-01 00:08:45 RD
6 2015-01-01 00:10:45 RD
7 2015-01-01 00:12:45 RD
8 2015-01-01 00:14:45 RD
9 2015-01-01 00:16:45 RD
10 2015-01-01 00:18:45 RD
11 2015-01-01 00:20:45 RD
12 2015-01-01 00:22:45 RD
13 2015-01-01 00:24:45 RD
14 2015-01-01 00:26:45 RD
15 2015-01-01 00:28:45 RD
16 2015-01-01 00:30:45 RD
17 2015-01-01 00:40:45 BL
18 2015-01-01 00:42:45 BL
19 2015-01-01 00:44:45 BL
20 2015-01-01 00:46:45 BL
21 2015-01-01 00:48:45 BL
22 2015-01-01 00:50:45 BL
23 2015-01-01 00:52:45 BL
24 2015-01-01 00:54:45 BL
25 2015-01-01 00:56:45 BL
26 2015-01-01 00:58:45 BL
27 2015-01-01 01:00:45 BL
28 2015-01-01 01:46:45 RD
29 2015-01-01 01:50:45 RD
30 2015-01-01 01:52:45 RD
Where the cur.gas column indicated which line (Red (RD) or blue (BL)) is feeding the sample into the analyser. I want to create horizontal bar plot from this data that would show me the colour corresponding to the current line (red and blue).
I have tried using ggplot:
ggplot(data=data1, aes(x=0.1, y = date, col=cur.gas)) +
geom_bar(stat = "identity", width = 0.1) +
coord_flip() +
scale_fill_manual(values = c("red","blue")) +
theme(text = element_text(size = 10), axis.title = element_blank(), axis.text = element_blank(),
axis.ticks = element_blank())
And the output plot looks like this:
Which looks horible, and the colours are wrong (the legend gives red colour for BL and blue for RD).
Then I tried the barplot function:
attach(data1)
barplot(date,names.arg=cur.gas, horiz = TRUE, col = c("blue", "red"), legend = rownames(data1$cur.gas))
and I got this error:
Error in barplot.default(date, names.arg = cur.gas, horiz = TRUE, col = c("blue", :
'height' must be a vector or a matrix
I'm lost now. What should I do? Thanks a lot1
What barplot wants is INTERVALS.
data[,"end_date"] <- c(data[-1,1], as.POSIXct("2015-01-01 01:54:45")) # I deciced a last value arbitrarily
data[,"interval"] <- difftime(data$end_date, data$date)
difftime(data$date[1], tail(data$end_date, n=1), units="min") # total 114 min
barplot(matrix(data$interval, ncol=1), horiz=T, col=c(2,4)[data$cur.gas], axes=F)
axis(1, at=seq(0,114,10), las=2, cex.axis=0.8, # If you don't need border, add border=NA
labels=format(seq(data[1,1], tail(data$end_date, n=1), by="10 min"),"%H:%M:%S"))
But it is more easy to use geom_rect() (this code needs end_date)
ggplot(data) +
geom_rect(aes(xmin=date, xmax=end_date, ymin=0, ymax=1, fill=cur.gas), colour="black") +
theme(axis.text.y=element_blank(), axis.ticks.y=element_blank()) + labs(x="time")
# if you don't need border, delete colour="black".