ggplot2: Combing 2 point graphs together - r

I want to create a plot that has point data (with error bars) from one data frame, and point data (coloured by gradient) from another. I can't seem to combine these plots and wondered if anyone had any suggestions. Plots are as follows:
mean_data<-data.frame(mean.x=c(1,2,3),mean.y=c(5,3,4),SE.y=c(-0.1,-0.15,-0.12),SE.x=c(-0.1,-0.15,-0.12))
x<-runif(15,min=0.1,max=5)
y<-runif(15,2,6)
grad<-1:15
point_data<-data.frame(x,y,grad)
ggplot(point_data,aes(x,y,colour=grad))+
geom_point(size=4)+
theme_classic()
ggplot(mean_data,aes(mean.x,mean.y))+
geom_point(size=3)+
geom_errorbar(aes(ymin = mean.y-SE.y, ymax = mean.y+SE.y),
position = position_dodge(0.3), width = 0.05)+
geom_errorbar(aes(xmin = mean.x-SE.x, xmax = mean.x+SE.x),
position = position_dodge(0.3), width = 0.05)+
theme_classic()
Plots are as follows:
Thanks!

You can combine them by using each dataframe to the corresponding geom and using mapping like this:
mean_data<-data.frame(mean.x=c(1,2,3),mean.y=c(5,3,4),SE.y=c(-0.1,-0.15,-0.12),SE.x=c(-0.1,-0.15,-0.12))
x<-runif(15,min=0.1,max=5)
y<-runif(15,2,6)
grad<-1:15
point_data<-data.frame(x,y,grad)
library(ggplot2)
ggplot() +
geom_point(point_data, mapping = aes(x,y,colour=grad), size = 4) +
geom_point(mean_data, mapping = aes(mean.x,mean.y), size = 3) +
geom_errorbar(mean_data, mapping = aes(x = mean.x, y = mean.y,
ymin = mean.y-SE.y, ymax = mean.y+SE.y), position = position_dodge(0.3), width = 0.05) +
geom_errorbar(mean_data, mapping = aes(x = mean.x, y = mean.y,
xmin = mean.x-SE.x, xmax = mean.x+SE.x), position = position_dodge(0.3), width = 0.05) +
theme_classic()
Created on 2022-08-15 by the reprex package (v2.0.1)

Related

Can't get errorbar to work with position_dodge with geom_point

I am trying to build a plot with several different data points. The first lot of data I am trying to display are the means with the confidence intervals. The second lot of data will be all the points as a jitter. The issue I am encountering is that I can't get the errorbars to position_dodge along with the geom_point, it just remains centered.
I've created other plots with no trouble, the only difference I can tell is that I am trying to specify the geom_point shape, so that I can have a black outline around the point for easier identification.
ggplot(NULL, aes(x, y)) +
geom_errorbar(
watervol.time,
mapping = aes(
x = Date.Order,
y = water.vol.mean,
ymin = LCI,
ymax = UCI
),
colour = "black", width = 10,
position = position_dodge()
) +
geom_point(
watervol.time,
mapping = aes(
x = Date.Order,
y = water.vol.mean,
fill = Site, group = Site
),
size = 3.5, shape = 21,
position = position_dodge(11)
)
Output that I'm getting:
EDIT: sorry, I couldn't figure out any other way to share the data. Values have been changed due to privacy:
Site Date.Order water.vol.mean LCI UCI
Quartz 28/09/2021 27.52666667 23.86938796 31.18394537
Quartz 29/11/2021 44.23333333 40.57605463 47.89061204
Quartz 18/07/2022 45.23666667 41.57938796 48.89394537
Quartz 27/09/2022 40.46 36.80272129 44.11727871
Hematite 28/09/2021 33.18666667 29.52938796 36.84394537
Hematite 29/11/2021 45.65 41.99272129 49.30727871
Hematite 19/07/2022 45.50333333 41.84605463 49.16061204
Hematite 27/09/2022 42.14 38.48272129 45.79727871
Olivine 28/09/2021 26.21333333 22.55605463 29.87061204
Olivine 29/11/2021 43.84333333 40.18605463 47.50061204
Olivine 18/07/2022 44.58066667 40.92338796 48.23794537
Olivine 27/09/2022 39.778 36.12072129 43.43527871
Plagioclase 28/09/2021 32.81666667 29.15938796 36.47394537
Plagioclase 29/11/2021 46.18166667 42.52438796 49.83894537
Plagioclase 19/07/2022 47.66633333 44.00905463 51.32361204
Plagioclase 27/09/2022 44.89196667 41.23468796 48.54924537
Both geoms need to have the same grouping and dodge width. This is best done when you initialise the plot using ggplot()
I can't confirm this without a reproducible example, but my guess is:
ggplot(watervol.time,
aes(x = Date.Order,
y = water.vol.mean,
ymin = LCI,
ymax = UCI,
group = Site)) +
geom_errorbar(position = position_dodge(width = 11),
colour = "black", width = 10) +
geom_point(aes(fill = Site),
position = position_dodge(width = 11),
size = 3.5, shape = 21)
I figured it out. I had to put group into both the geom_errorbar and geom_point:
ggplot(NULL, aes(x, y)) +
geom_jitter(SWC, mapping = aes(x = Date.Order, y = Water_Vol, colour = Site),
width = 5) +
geom_errorbar(watervol.time, mapping = aes(Date.Order, y = water.vol.mean,
ymin = LCI, ymax = UCI, group = Site), width = 10,
position = position_dodge(10)) +
geom_point(data = watervol.time, mapping = aes(Date.Order, y = water.vol.mean,
fill = Site, group = Site), shape = 21, size = 3.5,
position = position_dodge(10)) +
scale_fill_manual(values = c('blue', 'green', 'yellow', 'red')) +
scale_colour_manual(values = c('blue', 'green', 'yellow', 'red'))

ggplotly not recognizing geom_rect fill from ggplot in R

I am trying to determine how to ensure that the fill of a geom_rect in ggplot2 is respected once wrapped in plotly::ggplotly().
Example:
I first create a data.frame that contains the values I'll use to generate my plot.
library(ggplot2)
library(plotly)
dat <- data.frame(provider = rep(c('a','b','c'),2),
category = c(rep(c('Inpatient'),3),rep(c('Outpatient'),3)),
revenue = runif(6,100,500),
background_col = rep(c('red','green','blue'),2)
)
Using just ggplot the background panel colors on the geom_rect are respected
ggplot(dat,aes(x=category,y=revenue)) +
geom_rect(data = dat,aes(fill = background_col),xmin = -Inf,xmax = Inf,
ymin = -Inf,ymax = Inf,alpha = 0.1) +
geom_bar(stat = 'identity') +
facet_grid(~provider)
But, when I wrap it with ggplotly, those background colors disappear.
ggplotly(ggplot(dat,aes(x=category,y=revenue)) +
geom_rect(data = dat,aes(fill = background_col),xmin = -Inf,xmax = Inf,
ymin = -Inf,ymax = Inf,alpha = 0.1) +
geom_bar(stat = 'identity') +
facet_grid(~provider))
Any thoughts? I'm not super familiar with all the intricacies of plotly, so any insights are helpful!
Not sure how to get this automatically, but one workaround that ggplotly bug is to use specific numbers in place of -Inf and Inf:
ggplotly(ggplot(dat,aes(x=category,y=revenue)) +
geom_rect(data = dat,aes(fill = background_col),xmin = 0,xmax = 3,
ymin = -25,ymax = 475,alpha = 0.1) +
geom_bar(stat = 'identity') +
facet_grid(~provider))

How can I re-arrange my multi-plot to reverse alphabetical order?

Fig1
I've made my figure(Fig1 above) and annotated it to my liking, but I want the assorted figures to be displayed in reverse alphabetical order according to the top label names. I can reverse the values of the x-axis, but "order()" and "title()" don't seem to produce the effect I'm aiming for here. Is there any way to tell R I want the multi plot ordered differently? Or, is this just something I'll have to edit myself in another program like Adobe?
Code might be sloppy, but I'm just starting to work in R.
library(tidypaleo)
data<-read.table('multiplot_r.txt',sep="\t",header=TRUE)
as_tibble(data)
zone_data <- tibble(ymin = 0, ymax = 9649, xmin = -Inf, xmax = Inf)
multi_plot +
geom_rect(mapping = aes(ymin = ymin, ymax = ymax, xmin = xmin, xmax =
xmax), data = zone_data, alpha = 0.2, inherit.aes = FALSE)
multi_plot <- ggplot(data, aes(x = value, y = Age)) +
geom_lineh() +
scale_y_reverse() +
facet_geochem_gridh(vars(param)) +
geom_hline(yintercept = c(2200,2800,5000,5500), linetype = "dashed",
colour = "red")+
labs(x = "d18O", y = "Years BP")
multi_plot
Set param as a factor and reverse the order.
library (tidyverse)
data <- data %>% mutate(param = fct_rev(param))

trouble combining geom_rect and facet_grid in ggplot

I want to shade part of the background in each facet of a simple plot. If I omit faceting and run geom_rect + geom_point, the expected results appear as shown in the MRE below. If I omit the rectangle and run geom_point + facet_grid, the expected 4 panels have each point in the correct facet. But when I combine geom_rect + geom_point + and facet_grid, the points in the first category and only those get plotted in every facet. What is going on please???
library(ggplot2)
set.seed(42)
syn.dat <- data.frame(
category.1 = as.factor(rep(c("1A", "1B"), each = 8)),
category.2 = as.factor(rep(rep(c("2A", "2B"), times = 2), each = 4)),
x = rep(-1:2, each = 4) + runif(8, max = .4),
y = rep(-1:2, each = 4) + runif(8, max = .4))
ggplot() +
geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = .5,
ymax = Inf), fill = "lightyellow") +
geom_point(data = syn.dat, aes(x = x, y = y)) +
facet_grid(cols = vars(category.1),
rows = vars(category.2))
I'm not totally sure about this, but it may be that you need to explicitly provide the data argument to ggplot itself, in order for facet_grid to correctly pick up all the values?
ggplot(syn.dat) +
geom_rect(aes(xmin = -Inf, xmax = Inf, ymin = 0.5, ymax = Inf), fill = "lightyellow") +
geom_point(aes(x = x, y = y)) +
facet_grid(rows = vars(category.2), vars(cols = category.1))

geom_rect missing when converting ggplot2 to ggplotly

I'm trying to put together a ggplotly graph with three elements (geom_point, geom_line, and geom_rect) and it looks fine in ggplot2. However, when I convert to ggplotly, the geom_rect disappears. I'm thinking it's something with the inherit.aes function?
The code to build the test data is below.
library(ggplot2)
library(plotly)
dates_seq = seq.Date(as.Date("2019-03-13"), as.Date("2019-04-21"), by = "1 day")
df = data.frame(ds = dates_seq,
y = rnorm(length(dates_seq), mean = 50, sd = 5),
yhat = rnorm(length(dates_seq), mean = 50, sd = 5)
)
df$yhat_lower = df$yhat - 5
df$yhat_upper = df$yhat + 5
gg <- ggplot(df, aes(x = ds, y = y)) +
labs(x = 'Date', y = 'Sales') +
geom_ribbon(aes(ymin = yhat_lower, ymax = yhat_upper), fill = 'blue',
alpha = 0.2,
na.rm = TRUE)
start_date = as.Date("2019-04-19")
gg <- gg +
geom_point(na.rm=TRUE) +
geom_vline(xintercept = as.numeric(as.Date(start_date - lubridate::days(1))), linetype = 2, color = "black") +
geom_line(aes(y = yhat), color = 'blue',
na.rm = TRUE) +
theme_classic()
promo_df = data.frame(xmin = c("2019-03-15", "2019-04-01"), xmax = c("2019-03-18", "2019-04-08"),
ymin = -Inf, ymax = Inf, Promo = "Yes")
promo_df$id = 1:nrow(promo_df)
gg = gg +
geom_rect(data=promo_df, inherit.aes=FALSE,
aes(xmin=as.Date(xmin),
xmax=as.Date(xmax),
ymin=ymin,ymax=ymax,
group=id, fill = factor(Promo)), alpha=0.2) +
scale_fill_discrete(name = "On Promo?")
The ggplot image shows the desired output with the geom_rect.
gg
And now the ggplotly version:
ggplotly(gg)
Is there any way to get the ggplotly image to look like the basic ggplot2 chart?
Clara is right with respect to ggplotly's inability to support the ymin/max parameters. The best work around is to just manually set the parameters equal to the scale of your previous (main) layer. So in this case, it would be equal to 0/65.

Resources