I want to display on the same graph a geom_line and the state (which is in a vector).
The data for example:
Timestamp;Value;State
20190618;1.2;UP
20190619;1.0;DOWN
20190620;1.1;UP
...
This is an example of what i'd like to obtain:
I know how to geom_line, i've already try to use geom_area but none of these try succeed.
Any help ? :-)
library(tidyverse)
ggplot(df) +
geom_rect(aes(xmin = Timestamp, xmax = lead(Timestamp),
ymin = 0, ymax = Inf,
fill = State), alpha = 0.2) +
geom_step(aes(Timestamp, Value))
# based on your data, after converting into table with Timestamp as a date
df <- structure(list(Timestamp = structure(c(18065, 18066, 18067), class = "Date"),
Value = c(1.2, 1, 1.1), State = c("UP", "DOWN", "UP")),
class = "data.frame", row.names = c(NA, -3L))
Is this what you are looking for (alternative to geom_rect)?
Prepare example data
x <- 1:5
y <- c(4,1,6,2,2)
plot.df <- data.frame(Timestamp=x, Value=y)
Code for the plot:
library(ggplot2)
ggplot(plot.df, aes(x=Timestamp,y=Value)) +
annotate("rect", xmin = 1, xmax = 2, ymin = -Inf, ymax = Inf,
alpha = .2, fill = "green") +
annotate("rect", xmin = 2, xmax = 3, ymin = -Inf, ymax = Inf,
alpha = .2, fill = "red") +
annotate("rect", xmin = 3, xmax = 4, ymin = -Inf, ymax = Inf,
alpha = .2, fill = "green") +
geom_step(direction = "h") +
theme_classic()
Related
I want to plot two rectangles with internal gradients beside each other using ggpattern::geom_rect_pattern(pattern = "gradient") without a border around each rectangle.
Example:
library(tidyverse)
library(ggpattern)
tibble(
id = c("a", "b"),
xmin = c(-1, -1),
xmax = c(1, 1),
ymin = c(-1, 0),
ymax = c(0, 1)
) |>
ggplot() +
geom_rect_pattern(
aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, pattern_fill2 = id),
pattern_fill = "white", pattern = "gradient", pattern_orientation = "horizontal"
) +
theme_classic() +
coord_fixed(xlim = c(-1.1,1.1), ylim = c(-1.1,1.1), ratio = 1)
Which produces:
My issue is how do I remove the border around the rectangles?
Setting colour = "white" in geom_rect_pattern() will work to remove the outer border, but will introduce an internal border which is undesirable for my figure:
Setting colour = NA and/or pattern_colour = NA produces the same plot as the first
.
Is there an aesthetic I am missing here?
There seems to be a gray-filled rectGrob under the gradient fill on the finished plot, and you can just see the edges of it. If you set fill = NA this disappears.
library(tidyverse)
library(ggpattern)
tibble(
id = c("a", "b"),
xmin = c(-1, -1),
xmax = c(1, 1),
ymin = c(-1, 0),
ymax = c(0, 1)
) |>
ggplot() +
geom_rect_pattern(
aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, pattern_fill2 = id),
pattern_fill = "white", pattern = "gradient", fill = NA,
pattern_orientation = "horizontal",
) +
theme_classic() +
coord_fixed(xlim = c(-1.1,1.1), ylim = c(-1.1,1.1), ratio = 1)
I have a ggplot with means and both horizontal and vertical error bars, and I'd like to add a convex hull that encompasses all the error bars - like so:
I have tried with stat_chull from ggpubr but am not sure how to specify the aesthetics when there is a xmin, xmax, ymin and ymax for the error bars of each point. Below is how I coded the plot specifying the stat_chull aes as the X and Y means as an example - I know this is incorrect but I think I am on the right track?
ggplot()+
geom_point(data = df, aes(MeanX,MeanY)) +
geom_errorbar(data = df,
mapping = aes(x = MeanX,
ymin = MeanY - SdY,
ymax = MeanY + SdY),
width = 0, inherit.aes = FALSE)+
geom_errorbarh(data = df,
mapping = aes(y = MeanY,
xmin = MeanX - SdX,
xmax = MeanX + SdX),
height = 0, inherit.aes = FALSE)+
stat_chull(data = df, aes(MeanX,MeanY))+
theme_classic()
This gives the following plot:
I have also tried geom_polygon and got garbage.
Here is a the data:
df<-structure(list(Source = structure(1:5, .Label = c("A", "B", "C", "D",
"E"), class = "factor"), MeanX = c(-18.7066666666667,
-15.8769230769231, -16.8620689655172, -15.72, -17.4333333333333
), SdX = c(1.61072554509115, 0.409201849758959, 1.04811067886951,
0.74057035077327, 1.15902257671425), MeanY = c(9.93666666666667,
14.3230769230769, 9.22758620689655, 11.1, 13.7333333333333),
SdY = c(1.03005970142791, 0.539116085686704, 0.504990221704281,
0.757187779440037, 1.05039675043925)), row.names = c(NA,
-5L), class = "data.frame")
Any help is greatly appreciated!
Does the following work for you?
library(dplyr)
df %>%
mutate(ymin = MeanY - SdY,
ymax = MeanY + SdY,
xmin = MeanX - SdX,
xmax = MeanX + SdX) %>%
ggplot(aes(x = MeanX, y = MeanY))+
geom_point() +
geom_errorbar(aes(ymin = ymin, ymax = ymax),
width = 0)+
geom_errorbarh(aes(xmin = xmin, xmax = xmax),
height = 0)+
stat_chull(data = . %>%
summarise(x = c(MeanX, MeanX, MeanX, xmin, xmax),
y = c(MeanY, ymin, ymax, MeanY, MeanY)),
aes(x = x, y = y),
geom = "polygon", colour = "black", fill = NA)+
theme_classic()
Hello I used this code in order to get a ggplot figure :
ggplot(mod_mat_constraint, aes(x=Categorie, y=label)) + scale_fill_manual(values = c("#86d65e","#404040","#86d65e","#40c5e8","#e84a4a","#86d65e","#404040","#e2e2e2"), breaks=label_text) +
theme_tree2() + geom_tile(aes(fill = Value), colour = "black") + facet_wrap(~ facet) +
scale_y_discrete(limits=rev(list_tax_order))+
coord_cartesian(clip='off') +
annotation_custom(valigned,xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)+
theme(legend.position="none",plot.margin = unit(c(0, 12, 0, 0), "cm"))
so it just display a heatmap with a table next to it.
In order to add the the table I use then : annotation_custom(valigned,xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)
so I can move the table everywhere I want but I do not know how I can deal with the size of the df ?
I previously create the table with :
g1<-tableGrob(Resricted_df_heatmap3[1],rows = NULL)
g2<-tableGrob(Resricted_df_heatmap3[2],rows = NULL)
valigned <- gtable_combine(g1,g2, along=2)
valigned<- gtable_add_grob(valigned ,
grobs = rectGrob(gp = gpar(fill = NA, lwd = 3)),
t = 1, b = nrow(valigned), l = 1, r = ncol(valigned))
Reproducible data:
df <- data.frame(cbind("Thriving" = c(2, 2, NA, runif(9, 2.0, 5.0)), "Performance" = c(2, 3.5, 2.3, 4.2, NA, runif(7, 1.9, 6.9)), "Mastery_Climate" = c(runif(10, 2.2, 6.5), NA, 2.3), "Competitive_Climate" = c(NA, runif(4, 1.0, 3.6), NA, NA, runif(5, 1.5, 2.8)), "Collaboration" = c(runif(8, 2.2, 7.0), NA, NA, 5.5, 2.1)))
With this data I want to create bloxplots using the following command with the packages ggplot2 and tidyr:
df %>%
gather(key = "variable", value = "value") -> n
n$variable <- factor(n$variable, levels = c("Thriving", "Performance", "Mastery_Climate", "Competitive_Climate", "Collaboration"))
ggplot(data = n, aes(y = value, x = variable)) + stat_summary(fun.data = min.mean.sd.max, geom = "boxplot", col = "#323232", fill = "#EFC76C") +
coord_flip() + scale_y_continuous(breaks = c(1, 2, 3, 4, 5, 6, 7)) +
expand_limits(y = c(1, 7)) +
labs(x = "", y = "") +
theme(text = element_text(size = 12), panel.background = element_rect(fill = "#EAEDED")) +
theme(plot.margin=unit(c(0, 2, 0, 1.8),"cm"))
The function used in stat_summary is as follows:
min.mean.sd.max <- function(x) {
r <- c(min(x), mean(x) - sd(x), mean(x), mean(x) + sd(x), max(x))
names(r) <- c("ymin", "lower", "middle", "upper", "ymax")
r
}
Now, HERE IT COMES: everything works beautifully, however, now I would like to colour the background in three different colours, green, yellow and red. I know that I have to use geom_rect for that. However, in order to have the boxplots in the foreground, I need to pass the geom_rect argument first - but this breaks my code:
df %>%
gather(key = "variable", value = "value") -> n
n$variable <- factor(n$variable, levels = c("Thriving", "Performance", "Mastery_Climate", "Competitive_Climate", "Collaboration"))
ggplot(data = n, aes(y = value, x = variable)) +
geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = 3, fill = "green"), alpha = .01) +
geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = 3, ymax = 5, fill = "yellow"), alpha = .01) +
geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = 5, ymax = Inf, fill = "red"), alpha = .01) +
stat_summary(fun.data = min.mean.sd.max, geom = "boxplot", col = "#323232", fill = "#EFC76C") +
coord_flip() + scale_y_continuous(breaks = c(1, 2, 3, 4, 5, 6, 7)) +
expand_limits(y = c(1, 7)) +
labs(x = "", y = "") +
theme(text = element_text(size = 12), panel.background = element_rect(fill = "#EAEDED")) +
theme(plot.margin=unit(c(0, 2, 0, 1.8),"cm"))
As you can see, I get the error "Error: Discrete value supplied to continuous scale". From research I understand that this is because I needed to change the sequence and it now is a problem that the x-variable is a factor. However, I have been unable to solve this.
It would be great if all the other code could stay the same, it took me ages to put it together. Also, once the boxplots are in the foreground, it would be great if the grid behind would still be visible. Furthermore, I was so confused by the fill in geom_rect, I put in "green" and I get pink, or I put "yellow" and get blue - no clue why.
In any case, any help is very much appreciated. Many greetings!
Edit: Updated answer with better shading control
I think this approach is more idiomatic to ggplot: this puts the shading into a separate table with numeric y values. In a modified ggplot call, all the y values are mapped as numeric values, but the labels for those values are swapped out in the scale_y_continuous line.
rects <- data.frame(xmin = -Inf,
xmax = Inf,
ymin = c(0,3,5),
ymax = c(3,5,Inf),
fill = c("green", "yellow", "red"))
ggplot(data = n, aes(y = value, x = as.numeric(variable))) +
geom_rect(data = rects, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = fill),
# Control the shading opacity here.
inherit.aes = FALSE, alpha = 0.15) +
stat_summary(fun.data = min.mean.sd.max, geom = "boxplot", col = "#323232", fill = "#EFC76C") +
scale_fill_identity() +
scale_x_continuous(breaks = as.numeric(unique(n$variable)), minor_breaks = NULL,
labels = unique(n$variable)) +
scale_y_continuous(breaks = c(1, 2, 3, 4, 5, 6, 7)) +
expand_limits(y = c(1, 7)) +
coord_flip() +
labs(x = "", y = "") +
theme(text = element_text(size = 12), panel.background = element_rect(fill = "#EAEDED")) +
theme(plot.margin=unit(c(0, 2, 0, 1.8),"cm"))
Original answer
geom_rect's coordinates should be pulled outside of the aes() call, and then I get a working solution. However, one problem with this approach is that the background rectangles are actually getting drawn once for each element in the source data, which is why the colors are so bright even with alpha = 0.01.
ggplot(data = n, aes(y = value, x = variable)) +
geom_rect(xmin = -Inf, xmax = Inf, ymin = 0, ymax = 3, fill = "green", alpha = .005) +
geom_rect(xmin = -Inf, xmax = Inf, ymin = 3, ymax = 5, fill = "yellow", alpha = .005) +
geom_rect(xmin = -Inf, xmax = Inf, ymin = 5, ymax = 7, fill = "red", alpha = .005) +
stat_summary(fun.data = min.mean.sd.max, geom = "boxplot", col = "#323232", fill = "#EFC76C") +
coord_flip() + scale_y_continuous(breaks = c(1, 2, 3, 4, 5, 6, 7)) +
expand_limits(y = c(1, 7)) +
labs(x = "", y = "") +
theme(text = element_text(size = 12), panel.background = element_rect(fill = "#EAEDED")) +
theme(plot.margin=unit(c(0, 2, 0, 1.8),"cm"))
I am trying to produce a variable number of rectangles (layers) in a ggplot of a zoo object. I would like to do this in a loop since I do not know ahead of time how many rectangles I will need. Here is a toy example.
library("zoo")
library("ggplot2")
set.seed(1)
y <- runif(50, min = 1, max = 2)
start <- as.numeric(as.Date("2018-01-01"))
x <- as.Date(start:(start + 49))
x.zoo <- zoo(y, order.by = x)
## Fill areas
bars <- data.frame(start = c(x[5], x[20], x[35]),
end = c(x[10], x[25], x[40]))
I can plot these manually with this code:
## Plot manually
print(autoplot.zoo(x.zoo, facets = NULL) +
geom_rect(aes(xmin = bars[1,1],
xmax = bars[1,2], ymin = -Inf, ymax = Inf),
fill = "pink", alpha = 0.01) +
geom_rect(aes(xmin = bars[2,1],
xmax = bars[2,2], ymin = -Inf, ymax = Inf),
fill = "pink", alpha = 0.01) +
geom_rect(aes(xmin = bars[3,1],
xmax = bars[3,2], ymin = -Inf, ymax = Inf),
fill = "pink", alpha = 0.01))
This gives me this desired image:
I tried using the loop below but it only plots the last bar. How do I do this??
## This didn't work but illustrates what I am trying to do
p = autoplot.zoo(x.zoo, facets = NULL)
for(i in 1:3) {
p = p + geom_rect(aes(xmin = bars[i,1],
xmax = bars[i,2], ymin = -Inf, ymax = Inf),
fill = "pink", alpha = 0.01)
}
print(p)
You don't need a loop. geom_rect is vectorised
autoplot.zoo(x.zoo, facets = NULL) +
geom_rect(aes(xmin = start, xmax = end, ymin = -Inf, ymax = Inf), data = bars, fill = "pink", alpha = 0.4, inherit.aes = FALSE)
One way to avoid the for loop is to convert x.zoo into a data.frame and map the data to geom_line. This way, you can map the bars data to geom_rect separately.
dat <- data.frame(index = index(x.zoo), data.frame(x.zoo))
ggplot() +
geom_rect(data = bars, aes(xmin = start, xmax = end, ymin =-Inf, ymax = Inf), fill = 'pink', alpha = .5) +
geom_line(data=dat, aes(x = index, y = x.zoo))