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?
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 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")
Given below code,I'm trying to visualise the following layered bubble charts:
points of all start stations. Sizes vary with the total number of pickups.
points of all end stations. Sizes vary with the total number of returns.I need end up with a ggplot object named p1, with alpha = 0.5 to both layers.
library(lubridate)
library(tidyverse)
nycbikes18 <- read_csv("data/2018-citibike-tripdata.csv",
locale = locale(tz = "America/New_York"))
nycbikes18
#> # A tibble: 333,687 x 15
#> tripduration starttime stoptime
#> <dbl> <dttm> <dttm>
#> 1 932 2018-01-01 02:06:17 2018-01-01 02:21:50
#> 2 550 2018-01-01 12:06:18 2018-01-01 12:15:28
#> 3 510 2018-01-01 12:06:56 2018-01-01 12:15:27
#> 4 354 2018-01-01 14:53:10 2018-01-01 14:59:05
#> 5 250 2018-01-01 17:34:30 2018-01-01 17:38:40
#> 6 613 2018-01-01 22:05:05 2018-01-01 22:15:19
#> 7 290 2018-01-02 12:13:51 2018-01-02 12:18:42
#> 8 381 2018-01-02 12:50:03 2018-01-02 12:56:24
#> 9 318 2018-01-02 13:55:58 2018-01-02 14:01:16
#> 10 1852 2018-01-02 16:55:29 2018-01-02 17:26:22
#> # … with 333,677 more rows, and 12 more variables:
#> # start_station_id <dbl>, start_station_name <chr>,
#> # start_station_latitude <dbl>, start_station_longitude <dbl>,
#> # end_station_id <dbl>, end_station_name <chr>,
#> # end_station_latitude <dbl>, end_station_longitude <dbl>,
#> # bikeid <dbl>, usertype <chr>, birth_year <dbl>, gender <dbl>
expected output
I tried below code but not sure how to fix the n side.
p1 <- nycbikes18
p1 <- ggplot(p1) +
geom_point(aes(start_station_longitude,start_station_latitude,
size=n), alpha = 0.5) +
geom_point(aes(end_station_longitude,end_station_latitude, size=n),
alpha = 0.5)
p1
You are overwriting your "start station" aesthetics in ggplot() with the "end station" aes in the first geom_point() call.
From your description what you want is something like:
ggplot(p1) +
geom_point(aes(start_station_longitude,start_station_latitude, size = n_start), alpha = 0.5) +
geom_point(aes(end_station_longitude,end_station_latitude, size = n_end), alpha = 0.5)
Although you improve your chances of getting help if you share a reproducible example and explain what error you are getting.
library(tidyverse)
library(lubridate)
library(stringr)
df <-
tibble(Date = as.Date(0:364, origin = "2017-07-01"), Value = rnorm(365)) %>%
mutate(Year = str_sub(Date, 1, 4),
MoFloor = floor_date(Date, unit = "month")) %>%
group_by(Year, MoFloor) %>%
mutate(MoAvgValue = mean(Value)) %>%
ungroup() %>%
group_by(Year) %>%
mutate(MinMoFloor = min(MoFloor),
MaxMoFloor = max(MoFloor),
YearAvgValue = mean(MoAvgValue))
#> # A tibble: 365 x 8
#> # Groups: Year [2]
#> Date Value Year MoFloor
#> <date> <dbl> <chr> <date>
#> 1 2017-07-01 -1.83 2017 2017-07-01
#> 2 2017-07-02 -2.13 2017 2017-07-01
#> 3 2017-07-03 1.49 2017 2017-07-01
#> 4 2017-07-04 0.0753 2017 2017-07-01
#> 5 2017-07-05 -0.437 2017 2017-07-01
#> 6 2017-07-06 -0.327 2017 2017-07-01
#> 7 2017-07-07 -1.28 2017 2017-07-01
#> 8 2017-07-08 0.280 2017 2017-07-01
#> 9 2017-07-09 1.24 2017 2017-07-01
#> 10 2017-07-10 0.0921 2017 2017-07-01
#> # ... with 355 more rows, and 4 more
#> # variables: MoAvgValue <dbl>,
#> # MinMoFloor <date>,
#> # MaxMoFloor <date>,
#> # YearAvgValue <dbl>
Let's first plot the data frame above.
ggplot(df, aes(MoFloor, MoAvgValue, group = Year)) +
facet_grid(~Year, scale = "free_x", space = "free_x") +
geom_point()
In my call to the facet_grid() function I added the arguments scale = "free_x" and space = "free_x" to get rid of empty white space on the plots.
When I go ahead and add geom_segment()s based on group_by()d data, the scale = "free_x" and space = "free_x" arguments are negated. The empty white space reappears!
ggplot(df, aes(MoFloor, MoAvgValue, group = Year)) +
facet_grid(~Year, scale = "free_x", space = "free_x") +
geom_point() +
geom_segment(data = df,
aes(x = min(MinMoFloor),
y = YearAvgValue,
xend = max(MaxMoFloor),
yend = YearAvgValue))
My df data frame is grouped by Year. Why doesn't the geom_segment() function recognize this when I enter (for example) the x = min(MinMoFloor) argument? geom_segment() is pulling the min(MinMoFloor) from the global column, instead of the grouped column. How do I geom_segment() to evaluate the MinMoFloor column as grouped data?
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)