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)
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 would like to create a continuous time-series line graph. However, I have NA values in my data so the typical output is discontinuous. I tried using the na.omit argument but an error appears
Error in charToDate(x) : character string is not in a standard
unambiguous format"
Here is my script:
test <- read.csv(
file=paste0("testdata.csv"),
stringsAsFactors = FALSE)
test$Date <- as.Date(test$Date)
ggplot(na.omit(test), aes(x=Date, y=A))+
geom_line(na.rm=TRUE)+
xlab("") + ylab("A")+
(scale_x_date(breaks=date_breaks("1 month"),labels=date_format("%b")))+
scale_y_continuous(expand = c(0, 0), limits = c(28, 31))+
geom_point(shape=1)+
theme_bw()
Aside from that, I would also like to create a second y-axis in the same plot. I used sec.axis argument. The data for this axis also has NA values. However, since the first part of the script is having problems, I can not confirm if my code works. Here is the additional code:
geom_line(aes(y = B/20, colour ="B")) +
scale_y_continuous(expand=c(0,0), sec.axis = sec_axis(~.*20, bquote(B)))+
geom_point(shape=0)
Here is a portion of my data
Date
A
B
2020-09-23
28.2
NA
2020-09-30
NA
0.192
2020-10-01
28.4
NA
2020-10-07
28.6
NA
2020-10-14
28.8
NA
2020-10-21
28
NA
2020-10-28
NA
0.136
2020-11-01
28.5
NA
2020-11-04
27.6
NA
2020-11-11
27.9
NA
2020-11-18
27.9
NA
2020-11-25
NA
0.184
2020-12-01
28.1
NA
2020-12-02
28.4
NA
2020-12-09
29
NA
I'm not sure that this is what you want that portion of data's B have a lot of NA's.
comment: if na.omit to portion of data, nothing left so I cannot proceed with na.omit.
test2 <- test %>% mutate(Date = as.Date(Date))
test3 <- test2 %>%
select(Date, A) %>%
na.omit
test3 %>%
mutate(Date = as.Date(Date)) %>%
ggplot(aes(x=Date, y=A))+
geom_line(na.rm=TRUE)+
xlab("") + ylab("A")+
(scale_x_date(breaks=date_breaks("1 month"),labels=date_format("%b")))+
scale_y_continuous(expand = c(0, 0), limits = c(28, 31))+
geom_point(shape=1)+
theme_bw() +
geom_line(data = test2, aes(x = Date,y = B/20, colour ="B")) +
scale_y_continuous(expand=c(0,0), sec.axis = sec_axis(~.*20, bquote(B)))+
geom_point(shape=0,data = test2,aes(y = B* 20, colour ="B"))
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?
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)