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.
Related
I have a chart built with ggridges, as below, for which I would like to reverse the date order:
To do this I've added the c_trans() function, as defined here, but it requires the day in posixct format, which appears to convert the date to a continuous scale, even when I define group as a factor:
ggplot( lengthCounts2 %>% filter(rwi == rwiFilter),
aes( x = len,
fill = date,
group= factor(date)
)
) +
stat_density( aes( weight = normalised,
y = date, # time_trans works with objects of class POSIXct only
height = after_stat(density)
),
geom = 'density_ridges',
position = 'identity',
adjust = 0.1
) +
scale_y_continuous(trans = rev_date) +
theme_minimal() +
theme( plot.title = element_text(hjust=0.5)
) +
# scale_fill_brewer(palette = "Pastel1") +
labs( title = glue("Sampled/normalised packet size distribution for rwi={rwiFilter} ({rwiText})\n"),
x ="Length (bytes)",
y = "Date"
)
So that I end up with which is clearly not what I want. Is there any way to reverse the y axis but keep it discrete?
Input data:
> lengthCounts2
# A tibble: 8,724 x 5
# Groups: date, rwi [6]
date rwi len n normalised
<dttm> <chr> <dbl> <int> <dbl>
1 2022-04-13 00:00:00 01 35 677 0.0000319
2 2022-04-13 00:00:00 01 40 3113138 0.147
3 2022-04-13 00:00:00 01 41 15078 0.000710
4 2022-04-13 00:00:00 01 42 2077 0.0000978
5 2022-04-13 00:00:00 01 43 2554 0.000120
6 2022-04-13 00:00:00 01 44 29190 0.00137
7 2022-04-13 00:00:00 01 45 2065 0.0000972
8 2022-04-13 00:00:00 01 46 2054 0.0000967
9 2022-04-13 00:00:00 01 47 2625 0.000124
10 2022-04-13 00:00:00 01 48 146334 0.00689
# ... with 8,714 more rows
Use:
ylim("20220427", "20220420", "20220413")
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 time serie data from two consecutive years (2017-2018), from january to december of each year. Then I need to plot the data from sept-17 to april-18.
I could do it with a very hand-made code, however I realize it could be done very much straightforward way with the packages availabe today for managing dates on plots (packages "scales", "lubridate", etc.)
Can someone help me to simplify my work for doing the second plot?
I will really appreciate it.
suppressWarnings(suppressMessages(library("tidyverse", quietly = T)))
dat <- tibble(
date = seq(as.Date("2017-01-01"), as.Date("2018-12-31"), by=1),
var = rgamma(length(date), shape=2, scale=2)) %>%
mutate(year = lubridate::year(date),
month = lubridate::month(date),
julian = lubridate::yday(date))
dat
#> # A tibble: 730 x 5
#> date var year month julian
#> <date> <dbl> <dbl> <dbl> <dbl>
#> 1 2017-01-01 12.9 2017 1 1
#> 2 2017-01-02 6.69 2017 1 2
#> 3 2017-01-03 6.11 2017 1 3
#> 4 2017-01-04 1.68 2017 1 4
#> 5 2017-01-05 1.22 2017 1 5
#> 6 2017-01-06 10.2 2017 1 6
#> 7 2017-01-07 5.13 2017 1 7
#> 8 2017-01-08 4.61 2017 1 8
#> 9 2017-01-09 3.79 2017 1 9
#> 10 2017-01-10 1.11 2017 1 10
#> # … with 720 more rows
dat %>%
ggplot() +
geom_line(aes(julian, var, color = factor(month), linetype=factor(year)))
dat %>%
filter((year == 2017 & month %in% c("9","10", "11", "12"))|
(year == 2018 & month %in% c("1", "2", "3"))) %>%
mutate(julian_AWS = ifelse(julian>=244, julian-243, julian+123)) %>%
ggplot() +
geom_line(aes(julian_AWS, var, color = factor(month), linetype=factor(year)))+
scale_x_continuous(breaks = c(1,#S
31,#O
61,#N
91,#D
121,#E
151,#F
181),#M
labels = c("Sep", "Oct", "Nov", "Dec", "Jan", "Feb", "Mar"))+
theme(axis.text.x=element_text(hjust=-1))
Created on 2019-05-05 by the reprex package (v0.2.1)
I don't think you need to delve into the julian date formats. See if this gets you what you need:
dat %>%
filter(date >= '2017-09-01', date < '2018-04-01') %>%
ggplot() +
geom_line(aes(date, var, color = factor(month), linetype = factor(year))) +
scale_x_date(date_breaks = "1 month", date_labels = "%b") +
theme(axis.text.x = element_text(hjust = -1))
For more info on date label formats, see ?strftime
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)