ggplot geom_segment() not added - r

I am plotting an dbRDA ordination with ggplot.
data:
df1 <- structure(list(dbRDA1 = c(-0.277603544609864, -0.360978808436824,
-0.233134911000267, -0.310476800969782, -0.0906489101103515,
-0.145001415653946, -0.0332934057167847, -0.0275012617442552,
1.09301352917172, 1.06620520659396, -0.706049468959193, -0.322962763102308,
0.0621994470608108, 0.286233107477088), dbRDA2 = c(-0.0544365381093796,
-0.396097172755407, -0.179335397101395, -0.181894682898319, -0.0332360598060943,
-0.133901893994658, -0.231316948737708, -0.104267201073184, -0.446406664141594,
-0.324349265794935, 0.218701171555884, -0.0300275899654245, 0.595616680358468,
1.30095156246375), place = structure(c(2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("N", "O"), class = "factor"),
placecol = c("hollow", "hollow", "hollow", "hollow", "hollow",
"hollow", "hollow", "hollow", "non-hollow", "non-hollow",
"non-hollow", "non-hollow", "non-hollow", "non-hollow"),
treat = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 1L, 1L), .Label = c("10", "A"), class = "factor"),
OTU = c(1.358, 5.94, 1.119, 2.113, 1.12, 0.604, 1.525, 0.712,
1.841, 3.203, 2.512, 1.087, 1.087, 1.506)), .Names = c("dbRDA1",
"dbRDA2", "place", "placecol", "treat", "OTU"), row.names = c("60759",
"60773", "60774", "60775", "60776", "60777", "60778", "60779",
"60780", "60781", "60782", "60783", "60784", "60785"), class = "data.frame")
And plot code:
rda.plot <- ggplot(df1, aes(x=dbRDA1, y=dbRDA2, color=factor(treat),shape=factor(place))) +
geom_point(size=sqrt(df1$OTU)/sqrt(max(df1$OTU))*10,
aes(fill=factor(treat), alpha = as.factor(place)))+
geom_point(size=sqrt(df1$OTU)/sqrt(max(df1$OTU))*10) +
geom_text(aes(label=row.names(df1)),size=3, position = position_nudge(y = -0.1)) +
geom_hline(yintercept=0, linetype="dotted") +
geom_vline(xintercept=0, linetype="dotted") +
scale_shape_manual(values=c(21,24)) +
scale_alpha_manual(values=c(1,0)) +
scale_colour_manual(values = c("indianred3","lightskyblue4"))+
scale_fill_manual(values =alpha(c("indianred3","lightskyblue4")))+
coord_fixed()
rda.plot
As it is seen, I have a lot of scaling and color codes, which is working, but (at least for myself) is quite confusing. It has arrived from following a bunch of different guidelines (I imagine it could be trimmed).
I want to add a final layer, corresponding to environmental vectors.
Environmental data frame:
vf_biplot <- structure(list(dbRDA1 = c(0.703799726303485, 0.108418920195407,
0.10268149348934, 0.393538121176574, -0.692749765298912), dbRDA2 = c(-0.507323360489587,
0.843331735032726, -0.549310027554416, -0.36248490838257, -0.291382798048848
), Envir = c("AA", "N",
"DOP", "Pm", "P")), .Names = c("dbRDA1", "dbRDA2",
"Envir"), row.names = c("AA", "N",
"DOP", "Pm", "P"), class = "data.frame")
My attempt to add the layer (inspired by stackoverflow):
rda.plot + geom_segment(data = vf_biplot,
aes(x = 0, xend = dbRDA1, y = 0, yend = dbRDA2),
arrow = arrow(length = unit(0.25, "cm")), colour = "grey")+
geom_text(data = vf_biplot, aes(x = dbRDA1, y = dbRDA2, label = Envir),
size = 3)
I get this error:
Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?
I checked with str() but neither vf_biplot or df1 is a list (or containing lists).

Related

Error when trying to map aesthetics with geom_signi

I am trying to create a faceted boxplot with significance levels indicated as asterisks like '***'.
The problem is, I am getting an error when trying to add the geom_signif layer.
Warning message:
Ignoring unknown aesthetics: xmin, xmax, annotations, y_position, map_signif_level.
This is my data:
veg_un <- structure(list(Datum = structure(c(3L, 3L, 1L, 1L, 3L, 3L, 2L,
3L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 3L, 1L, 2L, 2L, 2L), .Label = c("2021-04-08",
"2021-05-17", "2021-07-07"), class = "factor"), Soll = c("1192",
"1192", "149", "2484", "552", "172", "1192", "1189", "2484",
"552", "552", "552", "119", "1192", "2484", "1202", "149", "552",
"1202", "1202"), Entfernung = structure(c(2L, 1L, 1L, 2L, 2L,
2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("2",
"5"), class = "factor"), DGUnkraut = c(0, 1.3, 0.3, 3.4, 0, 2.5,
4, 0, 1, 0.9, 0, 0.8, 0.5, 3, 1, 0.2, 0.2, 4, 0.5, 5)), row.names = c(NA,
-20L), class = "data.frame")
And this is my code so far.
library(tidyverse)
library(ggsignif)
library(ggpubr)
anno_df <- compare_means(DGUnkraut ~ Entfernung, group.by = "Soll", data = veg_un, p.adjust.method = "holm") %>%
mutate(y_pos = 7, p.adj = format.pval(p.adj, digits = 2))
ggplot(veg_un, aes(x=Entfernung, y=DGUnkraut)) +
geom_boxplot(position=position_dodge()) +
geom_point(aes(color=Entfernung), position=position_jitterdodge()) +
facet_wrap(~Soll) +
theme_minimal()+
ggsignif::geom_signif(
inherit.aes = F,
data=anno_df,
aes(xmin=group1, xmax=group2, annotations=p.adj, y_position=y_pos, map_signif_level = T),
manual=TRUE)
I don't know why this is happening. Also, the p-values are way too high. I tried to modify this with y_position, but since I can't control the aesthetics, it doesn't work.
I would try one of these
anno_df <- compare_means(DGUnkraut ~ Entfernung, group.by = "Soll", data = veg_un, p.adjust.method = "holm") %>%
mutate(y.position = 7, p.adj = format.pval(p.adj, digits = 2))
p <- ggplot(veg_un, aes(x=Entfernung, y=DGUnkraut)) +
geom_boxplot(position=position_dodge()) +
geom_point(aes(color=Entfernung), position=position_jitterdodge()) +
facet_wrap(~Soll) +
theme_minimal()
p + ggsignif::geom_signif(comparisons = list(c("2", "5")), map_signif_level = T)
p + ggpubr::stat_pvalue_manual(anno_df, label = "p.adj")
I see several things.
Your p.adj is 1 all the time. So I can't create a label with *
Although you are receiving some warnings I am not receiving any error and the code is doing what I expected.
You can resize the ylim and define the height of the labels.
library(tidyverse)
library(ggsignif)
library(ggpubr)
anno_df <- compare_means(DGUnkraut ~ Entfernung, group.by = "Soll", data = veg_un, p.adjust.method = "holm") %>%
mutate(y_pos = 5, label = format.pval(p, digits = 2))
ggplot(veg_un, aes(x=Entfernung, y=DGUnkraut)) +
geom_boxplot(position=position_dodge()) + ylim(0,7) +
geom_point(aes(color=Entfernung), position=position_jitterdodge()) +
facet_wrap(~Soll) +
theme_minimal()+
ggsignif::geom_signif(
inherit.aes = F,
data=anno_df,
aes(xmin=group1, xmax=group2, annotations=label, y_position=y_pos, map_signif_level = T),
manual=TRUE)
I obtained this image. I hope is what you were looking for.

ggplot 'race track' plot with polar_coord + points

I'm struggling to get polar_coords to work as I had hoped. I want each item to be represented by a coloured track, with a range of 1:50000. I then wanted to plot points over these tracks at the corresponding locations, with symbols representing the different categories. The points would then be annotated with the id.
Dataframe:
structure(list(item = structure(c(1L, 2L, 2L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L), .Label = c("AA", "AB", "AC", "AD", "AE",
"BA", "BB", "BC", "BD", "BE"), class = "factor"), location = c(10045L,
12041L, 15035L, 22054L, 19023L, 49411L, 39012L, 3041L, 23065L,
33015L, 42069L, 26859L), category = structure(c(1L, 1L, 2L, 3L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L), .Label = c("X", "Y", "Z"), class = "factor"),
id = structure(c(1L, 8L, 2L, 7L, 6L, 10L, 5L, 1L, 1L, 3L,
4L, 9L), .Label = c("Apple", "Banana", "Cherry", "Grape",
"Mango", "Melon", "Orange", "Pear", "Raspberry", "Strawberry"
), class = "factor")), .Names = c("item", "location", "category",
"id"), class = "data.frame", row.names = c(NA, -12L))
my_data %>%
ggplot(aes(item, location, shape = category, label = id)) +
geom_col(aes(y = Inf), fill = "gray80") +
geom_point(size = 3) +
geom_text(vjust = -1) +
scale_x_discrete(expand = expand_scale(add = c(5,0))) +
coord_polar(theta = "y") +
theme_void()
If you want a break in the middle, you could change the item to a numeric value relating to it's desired position:
my_data %>%
mutate(item_pos = as.numeric(item),
item_pos = item_pos + if_else(item_pos > 5, 1, 0)) %>%
ggplot(aes(item_pos, location, shape = category, label = id)) +
...
Maybe you can work from this:
ggplot(data,aes(x=location, color=id, y=id)) +
geom_linerange(aes(y=id, xmin=0, xmax=50000, color=category), size=2, alpha=0.5) +
geom_point(size=3) +
coord_polar()

Drawing percentage lines between bars in ggplot2

I have a bar chart which I want also to include some lines that show the percentage difference between them as in the following figure:
The lines in the figure are drawn just to make my point of what I ideally want.
Can someone help me with this?
Here is the dataframe to replicate the figure:
structure(list(shares = c(0.39, 3.04, 9.32, 22.29, 64.97, 0.01,
0.11, 5.83, 21.4, 72.64), quantile = structure(c(4L, 1L, 2L,
3L, 5L, 4L, 1L, 2L, 3L, 5L), .Label = c("2nd Quantile", "3rd Quantile",
"4nd Quantile", "Poorest 20%", "Richest 20%"), class = "factor"),
case = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L
), .Label = c("No Debt", "With Debt"), class = "factor")), row.names = c(NA,
-10L), class = "data.frame")
And here is my code used to make the bar plot:
ggplot(df_cum, aes(fill = case , quantile, shares)) + geom_bar(position =
"dodge", stat = "identity") +
scale_x_discrete(limits = c(
"Poorest 20%",
"2nd Quantile",
"3rd Quantile",
"4nd Quantile",
"Richest 20%"
)) +
theme_minimal()
Your data unchanged:
library(tidyverse)
df_cum<-structure(list(shares = c(0.39, 3.04, 9.32, 22.29, 64.97, 0.01,0.11, 5.83, 21.4, 72.64),
quantile = structure(c(4L, 1L, 2L, 3L, 5L, 4L, 1L, 2L, 3L, 5L),
.Label = c("2nd Quantile", "3rd Quantile", "4nd Quantile", "Poorest 20%", "Richest 20%"), class = "factor"),
case = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("No Debt", "With Debt"), class = "factor")), row.names = c(NA, -10L), class = "data.frame")
Your graph unchanged:
p <- ggplot(df_cum, aes(fill = case , quantile, shares)) +
geom_bar(position = "dodge", stat = "identity") +
scale_x_discrete(limits = c("Poorest 20%", "2nd Quantile", "3rd Quantile", "4nd Quantile", "Richest 20%")) +
theme_minimal()
I used the horizontal error bar to do the trick. Here is my solution:
y = rep(c(3, 5, 13, 25, 75),2)
x = rep(c(1:5), 2)
label = rep(c("-3%", "-5%", "-2%", "-1%", "10%"), 2)
p1 <- p + geom_text(x=x, y=y+2, label=label)
p1 + geom_errorbarh(aes(xmax = (x + 0.3), xmin = (x - 0.3), y = y), height = 0.5)
Now, you get:
You can also adjust both height and width if you like.

Credibility interval with respect two factors using ggplot2 in r

I have problem ploting credibility interval like this:
My data structure is following,L1,L2,M,U1,U2 stand for 0.025quant,0.25quant,0.5quant,0.75quant,0.975quant,respectively.
`
structure(list(approach = structure(c(1L, 2L, 1L, 2L, 1L, 2L), class = "factor", .Label = c("INLA",
"rjags")), param = structure(c(1L, 2L, 3L, 1L, 2L, 3L), class = "factor", .Label = c("alpha",
"beta", "sig2")), L1 = c(0.0844546867936143, 1.79242348175439,
0.163143886545317, 0.0754165380733685, 1.79067991488052, 3.66675821267498
), L2 = c(0.60090835904286, 1.95337968870806, 0.898159977552433,
0.606017177641373, 1.95260448314298, 4.07080184844179), M = c(0.870204161297956,
2.03768437879748, 2.20651061559405, 0.87408237273113, 2.03725552264872,
4.32531027636171), U2 = c(1.13905085248391, 2.12210930874551,
4.26836270504725, 1.66260576926063, 2.28900567640091, 5.10063756831338
), U1 = c(1.65214011950274, 2.28396345192398, 4.9109804477583,
1.1450384685802, 2.12117799328209, 4.55657971279654), AP = structure(c(1L,
4L, 5L, 2L, 3L, 6L), .Label = c("INLA.alpha", "rjags.alpha",
"INLA.beta", "rjags.beta", "INLA.sig2", "rjags.sig2"), class = "factor")), .Names = c("approach",
"param", "L1", "L2", "M", "U2", "U1", "AP"), row.names = c(NA,
-6L), class = "data.frame")`
I referenced this answerenter link description here,but 'fill' seems only work for boxplot case.the code I tried so far is:
CI$AP=interaction(CI$approach,CI$param)
p=ggplot(CI,aes(y=AP))+geom_point(aes(x=M))
p=p+geom_segment(aes(x=L1,xend=U1,y=AP,yend=AP))
p=p+geom_segment(aes(x=L2,xend=U2,y=AP,yend=AP),size=1.5)
It is far away from what I want.
Many thanks!
How about the following:
ggplot(df, aes(x = param, y = M, colour = approach)) +
geom_point(position = position_dodge2(width = 0.3), size = 3) +
geom_linerange(
aes(ymin = L2, ymax = U2, x = param),
position = position_dodge2(width = 0.3),
size = 2) +
geom_linerange(
aes(ymin = L1, ymax = U1, x = param),
position = position_dodge2(width = 0.3),
size = 1) +
coord_flip() +
labs(x = "Parameter", y = "Estimate")
Sample data
df <- structure(list(approach = structure(c(1L, 2L, 1L, 2L, 1L, 2L), class = "factor", .Label = c("INLA",
"rjags")), param = structure(c(1L, 2L, 3L, 1L, 2L, 3L), class = "factor", .Label = c("alpha",
"beta", "sig2")), L1 = c(0.0844546867936143, 1.79242348175439,
0.163143886545317, 0.0754165380733685, 1.79067991488052, 3.66675821267498
), L2 = c(0.60090835904286, 1.95337968870806, 0.898159977552433,
0.606017177641373, 1.95260448314298, 4.07080184844179), M = c(0.870204161297956,
2.03768437879748, 2.20651061559405, 0.87408237273113, 2.03725552264872,
4.32531027636171), U2 = c(1.13905085248391, 2.12210930874551,
4.26836270504725, 1.66260576926063, 2.28900567640091, 5.10063756831338
), U1 = c(1.65214011950274, 2.28396345192398, 4.9109804477583,
1.1450384685802, 2.12117799328209, 4.55657971279654), AP = structure(c(1L,
4L, 5L, 2L, 3L, 6L), .Label = c("INLA.alpha", "rjags.alpha",
"INLA.beta", "rjags.beta", "INLA.sig2", "rjags.sig2"), class = "factor")), .Names = c("approach",
"param", "L1", "L2", "M", "U2", "U1", "AP"), row.names = c(NA,
-6L), class = "data.frame")

R: ggplot annotate geom_text with position_dodge and facets

I have an R dataframe data (made with dplyr) that I'm trying to plot with ggplot():
require(dplyr)
data <- structure(list(gGroup = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L), .Label = c("MC", "R", "UC"), class = "factor"),
Episode = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L), .Label = c("Morning", "Day", "Night", "24 hour"
), class = "factor"), variable = c("HF", "HF", "LF", "LF",
"HF", "HF", "LF", "LF", "HF", "HF", "LF", "LF"), parameter = c("RR",
"RT", "RR", "RT", "RR", "RT", "RR", "RT", "RR", "RT", "RR",
"RT"), mean = c(3.90575222833804, 4.24572828952087, 5.14491629837998,
3.88189313775535, 4.02908403079823, 3.91129824615597, 4.73913642980089,
3.63973850905423, 4.66445796048274, 4.21723744674943, 5.57765585365275,
4.01444148455851), sd = c(1.09129154084895, 1.43102672123806,
1.17782114274004, 1.33381488706382, 1.33497319178289, 1.22259231099975,
1.33329948427898, 1.09625319168102, 1.19876558625356, 1.73746797295816,
1.05862249404741, 1.91144835753868), se = c(0.199241664579179,
0.261268538538247, 0.215039736195078, 0.243520167060353,
0.471984298305965, 0.432251656867227, 0.471392553343098,
0.387584032867524, 0.215304655178374, 0.312058460044998,
0.190134212775724, 0.343306259564318)), .Names = c("gGroup",
"Episode", "variable", "parameter", "mean", "sd", "se"), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -12L), drop = TRUE, indices = list(
0:1, 2:3, 4:5, 6:7, 8:9, 10:11), group_sizes = c(2L, 2L,
2L, 2L, 2L, 2L), biggest_group_size = 2L, labels = structure(list(
gGroup = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("MC",
"R", "UC"), class = "factor"), Episode = structure(c(2L,
2L, 2L, 2L, 2L, 2L), .Label = c("Morning", "Day", "Night",
"24 hour"), class = "factor"), variable = c("HF", "LF", "HF",
"LF", "HF", "LF")), .Names = c("gGroup", "Episode", "variable"
), class = "data.frame", row.names = c(NA, -6L)))
Currently I'm using the following code to plot:
require(ggplot2)
require(ggthemes)
pd <- position_dodge(width=0.9)
p <- ggplot(data, aes(x = gGroup, y = mean, fill = variable)) +
facet_grid(parameter~Episode) +
geom_bar(stat="identity", position=pd) +
geom_errorbar(aes(ymin = mean-se, ymax = mean+se), width = .3, position=pd) +
theme_hc() + scale_fill_hc() +
labs(y = "Logit transform of spectral power (m/s2), mean±SE", x= NULL)
ann_text <- data.frame(gGroup = "MC", mean = 6, variable = "LF", parameter = "RR", Episode = "Day")
p + geom_text(aes(ymax = 6.5, width = .2), data = ann_text, label="*", position=pd)
This gives me the following plot:
I'm quite satisfied with the result, but as you can see the asterisk isn't aligned correctly. I looked it up online, and I read this and this and the manual.
Everyone I see the suggestions to use position=position_dodge(width=0.9), but this doesn't help for me. I tried hjust to maybe move the asterisk to the right position, but that's of no use either. Funny thing is that my error bars are aligned correctly.
I feel like I'm overlooking something very simple, but I cannot figure out what it is.
I'm using R 3.1.3 on OSX 10.10.2, and loading the newest versions of ggplot2 and ggthemes.
In order for position_dodge to work, there needs to be a reason to dodge. That is you need to change the ann_text appropriately with variable = c("LF", "HF"), so that there is a reason to dodge. Then just define the label appropriately. Below I assume you just want the * over the LF bar.
ann_text <- data.frame(gGroup = rep("MC",2),
mean = 6,
variable = c("LF", 'HF'),
label = c("*", ""),
parameter = "RR",
Episode = "Day")
p + geom_text(aes(ymax = 6.5, width = .2, label = label), data = ann_text, position=pd)

Resources