How to add ribbon/shading to density plot - r

This is the dput output.
structure(list(data = structure(list(stations = c("s1", "s2",
"s3", "s4", "s5", "s1", "s2", "s3", "s4", "s5", "s1", "s2", "s3",
"s4", "s5", "s1", "s2", "s3", "s4", "s5"), Mean = c(792.0666667,
830.0666667, 542.4666667, 311.3333333, NA, 535.3222222, 576.9855556,
510.0756667, 433.9747778, 347.35, 726.9027778, 798.3153333,
706.2138889, 593.0828889, 474.9132222, 991.5945841, 1044.328044,
693.3808187, 403.8107808, NA), stdev = c(189.6764965, 123.9226103,
115.742876, 70.03437251, NA, 300.4539788, 306.3421887, 274.6515927,
237.7002122, 191.230147, 197.9449891, 248.458886, 203.0634826,
156.500875, 124.8746002, 265.4692578, 190.0975192, 185.9667516,
119.4178709, NA), Lower = c(602.3901701, 706.1440564, 426.7237907,
241.2989608, NA, 234.8682435, 270.6433669, 235.424074, 196.2745655,
156.119853, 528.9577886, 549.8564473, 503.1504063, 436.5820139,
350.038622, 726.1253263, 854.2305244, 507.4140671, 284.3929099, NA),
Upper = c(981.7431632, 953.9892769, 658.2095426, 381.3677058, NA,
835.776201, 883.3277442, 784.7272594, 671.67499, 538.580147,
924.8477669, 1046.774219, 909.2773715, 749.5837639, 599.7878224,
1257.063842, 1234.425563, 879.3475703, 523.2286518, NA), Group =
c("Observation", "Observation", "Observation", "Observation",
"Observation", "Historical", "Historical", "Historical", "Historical",
"Historical", "Future", "Future", "Future", "Future", "Future",
"Downscaled", "Downscaled", "Downscaled", "Downscaled", "Downscaled")),
class = "data.frame", row.names = c(NA, -20L)), layers =
list(<environment>, <environment>), scales = <environment>,
mapping = structure(list(x = ~Mean, Group = ~Group), class =
"uneval"), theme = list(), coordinates = <environment>, facet =
<environment>, plot_env = <environment>, labels = list(title =
"Probability density functions (PDF) for in comparison with
observations", y = "Probability density function", x = "March maximum
temperature", Group = "Group", colour = "factor(Group)", linetype =
"factor(Group)", fill = structure("fill", fallback = TRUE), weight =
structure("weight", fallback = TRUE))), class = c("gg",
"ggplot"))
I have produced density plot using ggplot2. The density plot is the mean. I want to shade each density line by lower and upper bounds just like confidence intervals. Please can anyone help how to achieve this?
The code I used to generate density in ggplot
ggplot(pdf1, aes(x=Mean, Group=Group)) +
geom_density(aes(colour=factor(Group), linetype=factor(Group)),
show.legend = FALSE) +
stat_density(aes(x=Mean, colour=factor(Group),
linetype=factor(Group)),
geom="line",position="identity", linewidth=1) +
xlab("March maximum temperature") +
ylab("Probability density function") +
ggtitle("Probability density functions (PDF) for in comparison with
observations") +
scale_linetype_manual(values=c('solid', 'solid','solid','dotted'),
labels=c("Observation", "Historical", "Future",
"Downscaled")) +
scale_colour_manual(values = c("black", "green", "blue", "red"),
labels=c("Observation", "Historical", "Future",
"Downscaled"))
This is the output from the above code
I searched in the net and could not find the solution.
Thanks a lot for your help!
This is a kind of output I am looking for (https://rpubs.com/Grady/875225). But as you can see, it has both x and y variables but I only have one variable (Mean in my case) in the x-axis while it is probability density in the y-axis in the above ggplot. So, I want to use the lower and upper values to envelope each line with shades with each respective color of my density plot

Related

point labels in R scatter plot

I have the following toy data
Xeafield (1999) (PER) ,1,0.5745375408
Lancelot et al. (1989),0.9394939494,0.4733405876
LemLM Xeafield (1997) (TER) ,0.6265126513,0.2959738847
Almore and Flemin (2001) (KER),0.4218921892,0.5745375408
Malek et al. (2006) (HER) ,0.4125412541,1
Charles and Osborne (2003),0.0308030803,0.1414581066
And trying a simple 2D plot in R with points labeled using the 1st column.
pdf('data.pdf', width = 7, height = 8)
d1 <- read.csv("data.csv", header=F, dec=".",sep = ",")
plot(as.matrix(d1[,2]), as.matrix(d1[,3]), col= "blue", pch = 19, cex = 1, lty = "solid", lwd = 2, ylim=c(0,1), xaxt = "n",yaxt = "n")
text(as.matrix(d1[,2]), as.matrix(d1[,3]), labels=as.matrix(d1[,1]), cex= 0.7, pos=3)
x_axis_range <- c(0,1)
x_axis_labels <- c("Small","Large")
axis(1,at = x_axis_range, labels = x_axis_labels)
y_axis_range <- c(0,1)
y_axis_labels <- c("Slow","Fast")
axis(2,at = y_axis_range, labels = y_axis_labels)
title(xlab="Memory", ylab="Speed",cex.lab=1)
dev.off()
But the plot doesn't come out right. A few issues I have: the axis label are messed up (it shows as.matrix ..., instead of the label I specified), and the margin of the plot is to small that node labels are cutoff. I am new to using R and plot, appreciate your help.
A simple solution for your problem is to define axis labels and axis ranges in the plot function.
d1 <- structure(list(V1 = structure(c(6L, 3L, 4L, 1L, 5L, 2L), .Label = c("Almore and Flemin (2001) (KER)",
"Charles and Osborne (2003)", "Lancelot et al. (1989)", "LemLM Xeafield (1997) (TER) ",
"Malek et al. (2006) (HER) ", "Xeafield (1999) (PER) "), class = "factor"),
V2 = c(1, 0.9394939494, 0.6265126513, 0.4218921892, 0.4125412541,
0.0308030803), V3 = c(0.5745375408, 0.4733405876, 0.2959738847,
0.5745375408, 1, 0.1414581066)), .Names = c("V1", "V2", "V3"
), class = "data.frame", row.names = c(NA, -6L))
# Use xlab and ylab for axis labels and
# and xlim and ylim for setting axis ranges
plot(as.matrix(d1[,2]), as.matrix(d1[,3]), col= "blue", pch = 19,
cex = 1, lty = "solid", lwd = 2, ylim=c(-0.1,1.1), xaxt = "n",yaxt = "n",
xlab="Memory", ylab="Speed",cex.lab=1, xlim=c(-0.1,1.1))
text(as.matrix(d1[,2]), as.matrix(d1[,3]),
labels=as.matrix(d1[,1]), cex= 0.7, pos=3)
x_axis_range <- c(0,1)
x_axis_labels <- c("Small","Large")
axis(1,at = x_axis_range, labels = x_axis_labels)
y_axis_range <- c(0,1)
y_axis_labels <- c("Slow","Fast")
axis(2,at = y_axis_range, labels = y_axis_labels)

How to add a second vertical line in R package forestplot

I'd like to distinguish between statistical significance (OR = 1.0) and clinical significance (OR = 1.5) in my forest plot. I created this plot using the forestplot package, sample code below. Is adding a vertical line possible (while maintaining the line of no difference)?
library(forestplot)
test_data <- structure(list(
mean = c(NA, NA, 1, 0.5, 2),
lower = c(NA, NA, .5, .25, 1.5),
upper = c(NA, NA, 1.5, .75, 2.5)),
.Names = c("mean", "lower", "upper"),
row.names = c(NA, -5L),
class = "data.frame")
tabletext <- cbind(
c("", "Outcome", "Outcome 1", "Outcome 2", "Outcome 3"),
c("", "OR", "1 (0.5 - 1.5)", "0.5 (0.25 - 0.75)", "2.0 (1.5 - 2.5)"))
forestplot(tabletext,
test_data,
new_page = TRUE,
xlog = TRUE,
boxsize = .25
)
Is this what you were looking for?
forestplot(tabletext,
test_data,
new_page = TRUE,
xlog = TRUE,
grid = structure(c(log(1.5)),
gp = gpar(lty = 2, col = "#CCCCFF")),
zero = 1,
boxsize = .25)
A suboptimal (and not very elegant) solution could be: 1- creating an empty plot with no axis or labels, 2- then plot a vertical line (abline(v=1.5)) and 3- call your forestplot with new_page = F.

Putting x-axis labels directly under tick marks in barplots in R

I have a table (below) showing the percentage of tree species (categorical variable) present in a group experiment. My objective is to plot the percentage of tree species on the y-axis and 'Species' on the x-axis within a barplot.
Issue
My problem is that I am experiencing problems with formatting the x-axis correctly. My objective is to ensure that the x-axis labels for**'Species'** are:-
Positioned directly underneath their bar at the tick mark
Do not overlap onto the plotting area
If anyone can help solve this issue, I would be incredibly grateful.
R code
df <- leaf.percent[order(leaf.percent$Leaf.Percentge, decreasing = TRUE),]
Tree.labels<-c("Quercus robar", "Quercus Patraea",
"Deciduous", "Oak",
"Plant", "Shrub")
par(mar=c(6, 6, 3, 3))
Tree<-barplot(df$Leaf.Percentge, names.arg = df$Species,
xaxt = "n",
ylab="Percentage %",
xlab="Tree Species",
col="lightblue",
ylim = c(0, 60))
axis(1, at=Tree, labels=FALSE)
text(seq(1, 6, by=1), par("usr")[3] - 0.2,
labels=unique(Tree.labels),
srt = 25, pos = 1,
xpd = TRUE, cex=0.7)
DATA
structure(list(Species = structure(1:6, .Label = c("Deciduous",
"Oak", "Plant", "Quercus_petraea", "Quercus_robur", "Shrub"), class = "factor"),
Frequency = c(48L, 29L, 6L, 70L, 206L, 4L), Leaf.Percentge = c(13.2231404958678,
7.98898071625344, 1.65289256198347, 19.2837465564738, 56.7493112947658,
1.10192837465565)), .Names = c("Species", "Frequency", "Leaf.Percentge"
), row.names = c(NA, -6L), class = "data.frame")

plot different values for same x axis value without overlapping using R

I am trying to plot parameter estimates from 2 different models on the same plot with confidence intervals. The values for each parameter are overlapping and I would like to have the values for each x value (parameter) side by side.
Data
ci_glm<- dput(head(ci_glm))
structure(list(parameter = c(-0.989960390831752, 0.23802371512626,
-0.0616305811832892, -1.19145279737722, -0.39565631764158, -2.70713419498971
), lower = c(-1.76343111098339, -0.0384145902617419, -0.338256948651047,
-2.41452042708909, -1.56076899496423, -3.8540190563328), upper = c(0.037528250100757,
0.514419550437131, 0.214814325315589, -0.0336965457336884, 0.639688082685478,
-1.70838885452134), par = c("Intercept", "Vessel 2", "Vessel 3",
"10", "11", "13")), .Names = c("parameter", "lower", "upper",
"par"), row.names = c("(Intercept)", "as.factor(CruiseID)201502",
"as.factor(CruiseID)201503", "as.factor(Stratum)10", "as.factor(Stratum)11",
"as.factor(Stratum)13"), class = "data.frame")
ci_boot<-dput(head(ci_boot2))
structure(list(parameter = c(-1.23409264614473, NA, NA, -0.434928403121171,
-2.74151010196932, -0.361626461606862), lower = c(-1.99928925205138,
NA, NA, -2.16613527555384, -1.38979210854727, -3.9529283095427
), upper = c(-0.118870916073164, NA, NA, 0.0238247660480798,
0.860847808652077, -1.44129237641604), par = c("Intercept", "Vessel 2",
"Vessel 3", "10", "11", "13"), diff = c(1.88041833597822, NA,
NA, 2.18996004160192, 2.25063991719935, 2.51163593312666), diff1 = c(0.76519660590665,
NA, NA, 1.73120687243267, -1.35171799342205, 3.59130184793584
), diff2 = c(1.11522173007157, NA, NA, 0.458753169169251, 3.6023579106214,
-1.07966591480918)), .Names = c("parameter", "lower", "upper",
"par", "diff", "diff1", "diff2"), row.names = c("(Intercept)",
"1", "11", "as.factor(Stratum)10", "as.factor(Stratum)11", "as.factor(Stratum)13"
), class = "data.frame")
plot
require(plotrix)
plot(ci_glm$parameter, type='n', ylab="Parameter Estimate",xlab="Parameter",pch=20, axes=F,ylim=c(-4,4))
axis(1, at=1:6, labels=unique(ci_glm$par),las=3,cex.axis=0.8)
axis(2)
abline(h=0,lty=2,col="light gray")
plotCI(ci_glm$parameter,y=NULL,uiw = abs(ci_glm$upper-ci_glm$parameter),liw=abs(ci_glm$parameter-ci_glm$lower),
ui=NULL,li=NULL,err="y", sfrac=0.01,gap=0,slty=par("lty"),
add=T,scol="black",pch=18,pt.bg=par("bg",col="black"))
par(new=T)
plot(ci_boot$parameter, type='n',ylab="Parameter Estimate",xlab="Parameter",pch=20, axes=F,ylim=c(-4,4),col="red")
axis(2)
abline(h=0,lty=2,col="light gray")
plotCI(ci_boot$parameter,y=NULL,uiw = abs(ci_boot$upper-ci_boot$parameter),liw=abs(ci_boot$parameter-ci_boot$lower),
ui=NULL,li=NULL,err="y", sfrac=0.01,gap=0,slty=par("lty"),
add=T,scol="red",pch=18,pt.bg=par("bg",col="red"))
leg.text<-c("GLM","GLMM")
legend("bottomright",leg.text,lty=c(1,1),pch=c(20,20),text.col="black",col=c("black","red"),bty="y",cex=.8)
R version 3.3.1 (2016-06-21)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
If you are willing to switch to using ggplot2, this can be done easily with position_dodge(), as seen here. I am also using dplyr to combine the two model outputs into one plottable data.frame.
toPlot <-
bind_rows(boot = ci_boot %>% mutate(Parameter = row.names(.))
, glm = ci_glm %>% mutate(Parameter = row.names(.))
, .id = "Model")
ggplot(
toPlot
, aes(x = Parameter
, y = parameter
, ymin = lower
, ymax = upper
, col = Model)) +
geom_linerange(position = position_dodge(0.2)) +
geom_point(position = position_dodge(0.2)) +
theme(axis.text.x = element_text(angle = 45
, hjust = 1))
Note that, for a couple of your bootstrap model parameters, the estimate is somehow outside the range of your confidence interval. You will likely want to clean up the labels etc. (and either remove or supply values for 1 and 11), but this addresses the overlap question.

Single error bar for stacked graph equalling 100

I have a stacked bar graph that shows the differences in classes between skeleton and tissue. The total of the two will always be 100 and their standard errors are the same. As such, the top error bar is superfluous and adds confusion.
Is there a way to only have the standard error for the bottom group? This link shows how to get a single bar for the top of the stack but isn't quite what I need: Single error bar on stacked bar plot ggplot Thanks.
Code:
library(reshape2)
library(Rmisc)
library(ggplot2)
melt <- melt(file, id=c("TREATMENT", "Species"),
value.name="Amount", variable.name = "Class")
x1 <- summarySE(melt, measurevar = "Amount",
groupvars = c("Species", "TREATMENT", "Class"), na.rm=TRUE)
x2 <- within(x1,lit2 <- ave(Amount, Class, Species, FUN = cumsum))
p10 <- ggplot(x2, aes(y = Amount, x = Class, fill = TREATMENT)) +
geom_bar(stat = "identity", colour = "black") +
geom_errorbar(aes(ymin = lit2-se, ymax = lit2+se), size = .5, width = .25)
p10
Data:
structure(list(TREATMENT = c("SKELETON", "SKELETON", "SKELETON",
"SKELETON", "TISSUE", "TISSUE", "TISSUE", "TISSUE"), Species = c("A",
"A", "A", "A", "A", "A", "A", "A"), `1` = c(42.1958615095789,
73.6083881998577, 62.1025409404354, 21.5264243794993, 57.8041384904211,
26.3916118001423, 37.8974590595646, 78.4735756205007), `2` = c(46.9398719372755,
89.6865089817669, 55.9907366318623, 18.1145895471236, 53.0601280627245,
10.3134910182331, 44.0092633681377, 81.8854104528764), `3` = c(55.4637732254405,
75.0933095632366, 20, 18.402199079204, 44.5362267745594, 24.9066904367634,
80, 81.597800920796)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -8L), .Names = c("TREATMENT", "Species",
"1", "2", "3"))

Resources