  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # use ggplot2 to plot two lines with ribbons  » r » use ggplot2 to plot two lines with ribbons

By : user2954836
Date : November 22 2020, 10:33 AM
I think the issue was by ths following , Here's how you can add a ribbon. You can, of course, change the formulas for ymin and ymax to suit your needs: code :
``````ggplot(df, aes(x=1:length(v), y=v, group=t, colour=t)) +
geom_ribbon(aes(ymin=v-0.1*v, ymax=v+0.1*v, fill=t), alpha=0.2) +
geom_line()
`````` ## Use for loop to plot multiple lines in single plot with ggplot2

By : Harrrrold1985
Date : March 29 2020, 07:55 AM
around this issue Instead of ruuning a loop, you should do this the ggplot2 way. ggplot2 wants the data in the long-format (you can convert it with reshape2::melt()). Then split the lines via a column (here Var2).
code :
``````y <- matrix(rnorm(100), 10, 10)
require(reshape2)
y_m <- melt(y)

require(ggplot2)
ggplot() +
geom_line(data = y_m, aes(x = Var1, y = value, group = Var2))
`````` ## How to combine and modify ggplot2 legends with ribbons and lines?

By : Eduardo Aguilar Yépe
Date : March 29 2020, 07:55 AM
it should still fix some issue You can use your version to also get the correct linetype, if you put the linetype insidt the aes function. However, your code becomes even more cumbersome that way. Consider reshaping your data before calling ggplot. Then you don't have to worry about the legend at all.
code :
``````# reshape data ...
new_data\$Ta <- Ta
new_data\$zero <- 0
require(reshape2)
dta <- melt(new_data, id.vars="V", measure.vars=c("IlD", "IlL", "Ta"))
dta.lower <- melt(new_data, id.vars="V", measure.vars=c("IlD_fill", "zero", "Ta"))
dta.upper <- melt(new_data, id.vars="V", measure.vars=c("Ta", "IlL", "Ta"))
dta <- cbind(dta, lower=dta.lower\$value, upper=dta.upper\$value)
dta\$name <- factor(NA, levels=c("Illegal landfill owner's\nprofitable ratio\n",
"Waste owner's\nprofitable ratio",
"Official tax"))
dta\$name[dta\$variable=="IlD"] <- "Illegal landfill owner's\nprofitable ratio\n"
dta\$name[dta\$variable=="IlL"] <- "Waste owner's\nprofitable ratio"
dta\$name[dta\$variable=="Ta"] <- "Official tax"
``````
``````ggplot(dta, aes(x=V, y=value, ymin=lower, ymax=upper,
color=name, fill=name, linetype=name)) +
geom_line(size=1.2) + ylim(c(0, Ta*1.5)) +
geom_ribbon(alpha=.25, linetype=0) +
theme(axis.text.x = element_text(angle=0, hjust = 0),
axis.title = element_text(face = 'bold', size = 14),
title = element_text(face = 'bold', size = 16),
legend.position = 'right',
legend.title = element_blank(),
legend.text = element_text(size = 12),
legend.key.width = unit(2, 'cm'))+
scale_linetype_manual(values=c(4, 5, 1)) +
labs(title="Profitable ratio between the volume \nof illegally disposed waste \nand costs of illegal waste disposure",
x="Waste volume, cubic meters",
y="Cost per cubic meter, RUB")
`````` ## How to plot multiple lines quickly in R using plot or ggplot2

By : Dr House
Date : March 29 2020, 07:55 AM
around this issue Here is a version using melt from reshape2 and dplyr to fit adjust the data:
code :
``````n <- 4
df <-
data.frame(
Ind = 1:n
, vol_1 = rnorm(n)
, vol_2 = rnorm(n)
, vol_3 = rnorm(n)
, vol_4 = rnorm(n)
)

melted <-
melt(df, id.vars = "Ind") %>%
mutate(Time = as.numeric(gsub("vol_","",variable)))

ggplot(melted
, aes(x = Time
, y = value
, col = as.factor(Ind))) +
geom_line()
`````` ## How to plot fitted meta-regression lines on a scatter plot when using metafor and ggplot2

By : sharkshy
Date : March 29 2020, 07:55 AM
around this issue In the documentation for rma.uni of the package metafor it says:
code :
``````ggplot(dat, aes(x = ablat, y = yi, size = 1/vi, col = alloc))+
geom_point(data = dat, shape = 16) +
geom_line(data = dat,aes(x = ablat,y = preds\$pred, size = 1))
`````` ## ggplot2: standard error ribbons not matching the plot lines

By : Jan
Date : March 29 2020, 07:55 AM
Hope this helps Specifying the data frame and subsetting to a variable (e.g. with \$) can cause weird things to happen in ggplot. You never need such subsetting, as the data frame is already specified to a data parameter at some point (likely in the first ggplot(...) call). Data, like aesthetics, is inherited from ggplot to geoms and stats, too, so unless you're changing the data, there is no reason to worry about re-specifying its origin. (If you do want to change the data frame used, specify the data parameter of the relevant geom or stat.)
Dropping the m\$s will fix the problem. Rearranging and simplifying so the logic is more obvious,
code :
``````library(ggplot2)

m <- data.frame(sound = c("English", "English", "English", "English", "Silence", "Silence", "Silence", "Silence"),
diff = c("Difficult questions", "Easy questions", "Difficult questions", "Easy questions", "Difficult questions", "Easy questions", "Difficult questions", "Easy questions"),
Mean = c(0.283, 0.36, 0.183, 0.227, 0.19, 0.347, 0.197, 0.333),
SD = c(0.500558971667007, 0.34814947033031, 0.497195715406262, 0.447163568809774, 0.49804988833968, 0.361515576848703, 0.498812908487118, 0.373926502247132),
SE = c(0.100111794333401, 0.0696298940660619, 0.0994391430812524, 0.0894327137619548, 0.099609977667936, 0.0723031153697406, 0.0997625816974236, 0.0747853004494263),
check.names = FALSE)

ggplot(m, aes(x = sound, y = Mean, ymax = Mean + SE, ymin = Mean - SE, 