Rupesh - 1 year ago 160

R Question

What I have here are two graphs "PlotA" and "PlotB", however I want a combined graph with geom_pointranges showing points, geom_line showing the line and geom_ribbon showing the standard deviation.

`water <- c(35,40,42,46,48,50)`

depth <- c(1,2,3,4,5,6)

sd <- c(10,10,10,10,10,10)

dataA <- data.frame(depth, water, sd)

from <- c(0.5, 1.5, 2.5, 3.5, 4.5, 5.5)

to <- c(1.5, 2.5, 3.5, 4.5, 5.5, 6.5)

depth1 <- c(1,2,3,4,5,6)

water1 <- c(40,32,50,55,62,30)

dataB <- data.frame(from,to,depth1, water1)

# Load necessary packages

require(ggplot2)

# Plotting Started

#PlotA

ggplot(data=dataA, aes(x = water, y = depth), na.rm=T) +

geom_path(size=0.4, color="black")+

geom_pointrange(data=dataB, aes(water1, depth1, ymin=from, ymax=to), size=0.1, color='black') +

scale_y_reverse(lim = c(10,0), breaks = seq(0,10,1)) +

theme_bw(12) +

scale_x_continuous(lim =c(0,100), breaks = seq(0,100,20))

#PlotB

ggplot() + geom_ribbon(data=dataA, aes(x=depth, y=water, ymin = water - sd, ymax = water + sd), alpha=0.3, fill='grey12') + coord_flip() +

scale_x_reverse(lim = c(10,0), breaks = seq(0,10,1)) + theme_bw(12) +

scale_y_continuous(lim =c(0,100), breaks = seq(0,100,20))

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

`coord_flip`

is difficult to use well in the middle of a plot. I strongly recommend debugging plots without it and then adding it as the last step.

I think this is what you're looking for. If not, please describe your desired result in more detail.

```
ggplot(data = dataA, aes(x = depth, y = water)) +
geom_ribbon(
data = dataA,
aes(
x = depth,
ymin = water - sd,
ymax = water + sd
),
alpha = 0.3,
fill = 'grey12'
) +
geom_path(size = 0.4, color = "black") +
geom_point(
data = dataB,
aes(x = depth1, y = water1),
size = 0.1,
color = 'black'
) +
geom_errorbarh(
data = dataB,
aes(
x = depth1,
xmin = from,
xmax = to,
y = water1
),
size = 0.1,
height = 0
) +
theme_bw(12) +
scale_x_reverse(lim = c(10, 0), breaks = seq(0, 10, 1)) +
scale_y_continuous(lim = c(0, 100), breaks = seq(0, 100, 20)) +
coord_flip()
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**