Overlapping density plots in a ggtern ternary diagram - r

I have two datasets looking like the following:
#dataset 1
structure(list(dataset1 = 1:86, x = c(24.22055, 24.61821, 24.60858,
24.5963, 24.66904, 24.682, 24.74323, 24.84038, 25.02606, 25.00763,
24.99861, 25.00901, 24.99273, 24.98789, 24.99308, 24.97615, 24.9572,
24.95962, 24.93451, 25.08111, 24.97653, 24.92734, 24.96208, 25.03111,
25.00242, 24.95385, 24.99345, 25.03311, 24.93516, 24.95163, 24.94859,
25.07071, 25.15814, 25.22433, 25.3163, 25.22823, 25.34902, 25.4118,
25.40885, 25.35868, 25.34709, 25.24046, 25.31097, 25.32868, 25.41141,
24.92474, 24.90951, 24.9927, 25.0052, 24.94954, 25.15449, 25.10164,
25.03112, 24.97345, 25.03352, 25.11059, 25.05391, 25.05766, 25.06176,
25.17039, 25.17868, 25.1053, 25.0568, 25.08028, 25.137, 25.36559,
25.06363, 25.26306, 25.16708, 25.14826, 25.06046, 24.99418, 25.19738,
25.20072, 25.24073, 25.18705, 25.18142, 25.16747, 25.1235, 25.38767,
25.37099, 25.30558, 25.35074, 25.33528, 25.32482, 25.32328),
y = c(22.25462, 21.88752, 21.89172, 21.88356, 21.86319, 21.80782,
21.7451, 21.70914, 21.68861, 21.66829, 21.67942, 21.67475,
21.67994, 21.67462, 21.67405, 21.67494, 21.66842, 21.65091,
21.6657, 21.68427, 21.66878, 21.6711, 21.66772, 21.63123,
21.64916, 21.65174, 21.65686, 21.63292, 21.64039, 21.53591,
21.64633, 21.62177, 21.61304, 21.60609, 21.594, 21.60413,
21.59069, 21.58264, 21.58277, 21.57736, 21.57457, 21.57674,
21.56562, 21.49258, 21.48584, 21.74852, 21.73081, 21.75594,
21.66646, 21.70782, 21.67075, 21.66456, 21.64514, 21.65763,
21.66863, 21.64658, 21.63672, 21.62677, 21.65441, 21.61994,
21.61754, 21.65159, 21.62676, 21.61157, 21.60181, 21.65121,
21.61303, 21.61424, 21.61419, 21.6258, 21.59797, 21.61477,
21.5879, 21.58918, 21.61834, 21.56725, 21.61358, 21.61456,
21.57619, 21.592, 21.58095, 21.52847, 21.57284, 21.56755,
21.56847, 21.49455), z = c(53.52483, 53.49427, 53.49971,
53.52014, 53.46777, 53.51018, 53.51168, 53.45048, 53.28533,
53.32408, 53.32197, 53.31623, 53.32733, 53.33749, 53.33287,
53.34891, 53.37439, 53.38947, 53.39978, 53.23462, 53.35469,
53.40156, 53.3702, 53.33767, 53.34843, 53.39441, 53.34969,
53.33398, 53.42445, 53.51247, 53.40507, 53.30752, 53.22882,
53.16958, 53.0897, 53.16764, 53.06029, 53.00556, 53.00838,
53.06396, 53.07834, 53.1828, 53.12341, 53.17874, 53.10275,
53.32674, 53.35968, 53.25136, 53.32834, 53.34264, 53.17476,
53.2338, 53.32374, 53.36892, 53.29785, 53.24283, 53.30937,
53.31556, 53.28384, 53.20967, 53.20378, 53.24311, 53.31644,
53.30816, 53.26118, 52.9832, 53.32334, 53.1227, 53.21872,
53.22594, 53.34158, 53.39105, 53.21472, 53.2101, 53.14093,
53.2457, 53.205, 53.21797, 53.30031, 53.02033, 53.04806,
53.16595, 53.07643, 53.09717, 53.10672, 53.18217)), class = "data.frame", row.names = c(NA,
-86L))
#dataset2
structure(list(dataset2 = 1:16, x1 = c(24.702, 24.64061, 24.64624,
24.699, 24.68064, 24.65854, 24.75148, 24.58633, 24.73463, 24.59992,
24.65293, 24.60753, 24.62394, 25.3416, 24.71006, 24.67719), y1 = c(21.87799,
21.89606, 21.9034, 21.8859, 21.89083, 21.90291, 21.8491, 21.93269,
21.87262, 21.87465, 21.90029, 21.87801, 21.87661, 21.64635, 21.83719,
21.90565), z1 = c(53.42002, 53.46333, 53.45036, 53.4151, 53.42853,
53.43855, 53.39942, 53.48098, 53.39274, 53.52543, 53.44677, 53.51446,
53.49945, 53.01205, 53.45276, 53.41716)), class = "data.frame", row.names = c(NA,
-16L))
I have written a code to plot kernel density contours according to the ggtern package.
# density plot for dataset 1
plot1 <- ggtern(data = test,aes(x=x, y=y, z=z))
plot1+ stat_density_tern(geom="polygon",
aes(fill = ..level..,
alpha = ..level..)) +
theme_rgbw() +
labs(title = "Example Density/Contour Plot") +
scale_fill_gradient(low = "lightblue",high = "blue") +
guides(color = "none", fill = "none", alpha = "none")+
scale_T_continuous (limits = c(0.225,0.215))+
scale_L_continuous (limits= c(0.255,0.245))+
scale_R_continuous (limits = c(0.53,0.54))
# density plot for dataset 2
plot2 <- ggtern(data = test2,aes(x=x1, y=y1, z=z1))
plot2 + stat_density_tern(geom="polygon",
aes(fill = ..level..,
alpha = ..level..)) +
theme_rgbw() +
labs(title = "Example Density/Contour Plot") +
scale_fill_gradient(low = "lightgreen",high = "green") +
guides(color = "none", fill = "none", alpha = "none")+
scale_T_continuous (limits = c(0.225,0.215))+
scale_L_continuous (limits= c(0.255,0.245))+
scale_R_continuous (limits = c(0.53,0.54))
The next step I would like to do is to overlap plot1 with plot2. I was wondering if anyone knows how to achieve this. Thanks.

The easiest way to handle this is to add a column to both dataframes identifying the source of the data and then to combine then into 1 large data frame.
Then in the mapping definition define the "group" parameter.
#Add column to identify the data source
test1$id <- "Test1"
test2$id <- "Test2"
test2$z <- test2$z+0.2
test2$y <- test2$y+0.2
#combine both datasets into 1
names(test2)<-names(test1)
totalTest <- rbind(test1, test2)
#plot and group by the new ID column
plot1 <- ggtern(data = totalTest, aes(x=x, y=y, z=z, group=id, fill=id))
plot1+ stat_density_tern(geom="polygon",
aes(fill = ..level..,
alpha = ..level..)) +
theme_rgbw() +
labs(title = "Example Density/Contour Plot") +
scale_fill_gradient(low = "lightblue",high = "blue") +
guides(color = "none", fill = "none", alpha = "none") +
scale_T_continuous (limits = c(0.225,0.215))+
scale_L_continuous (limits= c(0.255,0.245))+
scale_R_continuous (limits = c(0.53,0.54))

Related

adding standard errors to correct panels on faceted bar chart

I have this dataframe:
structure(list(taxon = c("Acidaminococcus", "Butyricicoccus",
"Butyrivibrio", "Collinsella", "Coprococcus", "Olsenella", "Parabacteroides",
"Paraprevotella", "Pasteurellaceae_unclassified"), lfc_StatusChronic.ACST0. = c(0.88175957,
0.88803574, 0.790947444, 1.319321361, 0.7176503, 0.758374253,
-0.833877215, -1.106098414, 0.932218695), se_StatusChronic.ACST0. = c(0.439259504,
0.449835605, 0.369268494, 0.391714918, 0.27578621, 0.364036816,
0.377314959, 0.485078744, 0.421283473), lfc_Time.fT1 = c(-0.021243562,
0.66196107, 0.334274258, -0.382520121, -0.005363874, -0.313304181,
-0.439558941, -0.029316428, 0.682658747), se_Time.fT1 = c(0.312681188,
0.330173331, 0.301559494, 0.309355933, 0.293938402, 0.302957725,
0.339292487, 0.361459254, 0.385696553), lfc_Time.fT2 = c(-1.092105655,
-0.083635974, -0.435405323, -1.221704783, -0.557850053, -0.734425087,
-0.19277856, 0.148094198, 0.461233277), se_Time.fT2 = c(0.326565043,
0.344533883, 0.31544836, 0.323423323, 0.307225241, 0.317023725,
0.354270528, 0.377368442, 0.403530764), lfc_Time.fT3 = c(-0.684898535,
0.007779894, -0.661494348, -0.765693993, -0.294827229, -1.082174069,
-0.428338824, 0.072377208, 0.682615791), se_Time.fT3 = c(0.324919486,
0.342422134, 0.314578177, 0.322254678, 0.305999846, 0.316331693,
0.352370636, 0.375283079, 0.402530027), lfc_Time.fT4 = c(-1.038613852,
-0.159777157, -0.172345815, -0.691220321, -0.444048742, -1.062300665,
0.073495083, 0.295212326, 0.337145234), se_Time.fT4 = c(0.319416657,
0.336513636, 0.309526757, 0.316959694, 0.300928605, 0.311343927,
0.346365478, 0.36886735, 0.396117478), lfc_Time.fT5 = c(-0.714954683,
0.081376697, -0.621676699, -0.483698623, -0.339094441, -0.718106519,
-0.055315775, 0.475970869, 0.160939365), se_Time.fT5 = c(0.317230276,
0.334106044, 0.307553106, 0.314893819, 0.298943665, 0.309379791,
0.343965965, 0.366296439, 0.393607858)), row.names = c(NA, -9L
), class = "data.frame")
It is a dataframe where each row is a category, and the columns correspond with a time series (from T0 til T5).
I want to do a bar chart for each category (taxon) for their time (T0-T5):
melted_df <- reshape2::melt(taxonFC1, id.vars = "taxon", variable.name = "timepoint", value.name = "value")
ggplot(melted_df, aes(x = timepoint, y = value, fill = taxon)) +
geom_bar(stat = "identity") +
facet_wrap(~ taxon, ncol = 3) +
labs(title = "Bar Chart for Different Time Series",
x = "Time Point",
y = "Value",
fill = "Category")
The question is if it is possible to assign the standard error (se columns) to their logFC value (lfc columns) for each time series.
Update:
I did this, but only for T0:
ggplot(data = taxonFC1, aes(x = taxon, y = lfc_StatusChronic.ACST0., fill = taxon)) +
geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
xlab("Category") +
ylab("lfc_StatusChronic.ACST0.") +
ggtitle("Bar Plot of lfc_StatusChronic.ACST0. by Category") +
# Add error bars using se_StatusChronic.ACST0. column
geom_errorbar(aes(ymin = lfc_StatusChronic.ACST0. - se_StatusChronic.ACST0.,
ymax = lfc_StatusChronic.ACST0. + se_StatusChronic.ACST0.),
width = 0.4)
Output expected (the image is from other data):
Is this what you're looking for?
library(dplyr)
library(tidyr)
library(ggplot2)
dat <- structure(list(taxon = c("Acidaminococcus", "Butyricicoccus",
"Butyrivibrio", "Collinsella", "Coprococcus", "Olsenella", "Parabacteroides",
"Paraprevotella", "Pasteurellaceae_unclassified"), lfc_StatusChronic.ACST0. = c(0.88175957,
0.88803574, 0.790947444, 1.319321361, 0.7176503, 0.758374253,
-0.833877215, -1.106098414, 0.932218695), se_StatusChronic.ACST0. = c(0.439259504,
0.449835605, 0.369268494, 0.391714918, 0.27578621, 0.364036816,
0.377314959, 0.485078744, 0.421283473), lfc_Time.fT1 = c(-0.021243562,
0.66196107, 0.334274258, -0.382520121, -0.005363874, -0.313304181,
-0.439558941, -0.029316428, 0.682658747), se_Time.fT1 = c(0.312681188,
0.330173331, 0.301559494, 0.309355933, 0.293938402, 0.302957725,
0.339292487, 0.361459254, 0.385696553), lfc_Time.fT2 = c(-1.092105655,
-0.083635974, -0.435405323, -1.221704783, -0.557850053, -0.734425087,
-0.19277856, 0.148094198, 0.461233277), se_Time.fT2 = c(0.326565043,
0.344533883, 0.31544836, 0.323423323, 0.307225241, 0.317023725,
0.354270528, 0.377368442, 0.403530764), lfc_Time.fT3 = c(-0.684898535,
0.007779894, -0.661494348, -0.765693993, -0.294827229, -1.082174069,
-0.428338824, 0.072377208, 0.682615791), se_Time.fT3 = c(0.324919486,
0.342422134, 0.314578177, 0.322254678, 0.305999846, 0.316331693,
0.352370636, 0.375283079, 0.402530027), lfc_Time.fT4 = c(-1.038613852,
-0.159777157, -0.172345815, -0.691220321, -0.444048742, -1.062300665,
0.073495083, 0.295212326, 0.337145234), se_Time.fT4 = c(0.319416657,
0.336513636, 0.309526757, 0.316959694, 0.300928605, 0.311343927,
0.346365478, 0.36886735, 0.396117478), lfc_Time.fT5 = c(-0.714954683,
0.081376697, -0.621676699, -0.483698623, -0.339094441, -0.718106519,
-0.055315775, 0.475970869, 0.160939365), se_Time.fT5 = c(0.317230276,
0.334106044, 0.307553106, 0.314893819, 0.298943665, 0.309379791,
0.343965965, 0.366296439, 0.393607858)), row.names = c(NA, -9L
), class = "data.frame")
dat %>%
rename(lfc_time.fT0 = lfc_StatusChronic.ACST0.,
se_Time.fT0 = se_StatusChronic.ACST0.) %>%
pivot_longer(-taxon, names_pattern="(.*)_[Tt]ime\\.f(.*)",
names_to = c(".value", "time")) %>%
ggplot(aes(x = time, y = lfc, ymin = lfc - se, ymax = lfc + se, fill = taxon)) +
geom_bar(stat = "identity") +
geom_errorbar(width=.4) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_fill_brewer(palette="Set1") +
xlab("Category") +
ylab("lfc_StatusChronic.ACST0.") +
facet_wrap(~taxon, ncol=1) +
ggtitle("Bar Plot of lfc_StatusChronic.ACST0. by Category")```
If so, the key is to rename the T0 variables to have the same format as the other time-period variables and then use pivot_longer() to put all the lfc measures in a single column and all the se measures in a single column. The rest is accomplished with faceting on the time variable. The pivot_longer() documentation has some good examples of retaining multiple columns, see in particular the last example on the page.

Add confidence intervals with specific values to a graph line with two series

I want to reproduce the following graph:
And my data is the following, where the blue line is complete_preds_means and the orange line is contrafact:
structure(list(dias = structure(c(19052, 19053, 19054, 19055,
19056, 19057, 19058, 19059, 19060, 19061, 19062, 19063, 19064,
19065, 19066, 19067, 19068, 19069, 19070, 19071), class = "Date"),
complete_preds_means = c(341.07434, 381.59167, 455.47815,
485.05597, 527.60876, 562.63965, 602.48975, 624.663, 626.5637,
527.2239, 420.71643, 389.30804, 378.74396, 366.61548, 361.36566,
363.37253, 319.31824, 314.39688, 303.60342, 294.8934), contrafact = c(364.5,
358.89, 466.64, 470.11, 464.25, 487.27, 591.2, 715.33, 628.02,
505.98, 402.9, 316.81, 323.35, 358.61, 354.26, 369.5, 317.01,
336.5, 285.33, 270.91), complete_preds_lower = c(320.6368042,
361.7870895, 432.4487762, 461.2275833, 503.2255051, 535.7108551,
576.3850006, 597.9762146, 601.4407013, 504.0448837, 398.7777023,
368.0046799, 356.3603165, 345.5847885, 339.9679932, 342.7514801,
298.3247482, 293.4419693, 282.5286865, 275.4635284), complete_preds_upper = c(359.9897186,
402.5708664, 477.4746765, 508.7775711, 550.3326447, 587.6521027,
628.5320251, 649.9691833, 649.4831665, 547.9886108, 442.046402,
410.8121475, 399.0208908, 389.8615128, 387.4929993, 386.2935928,
340.140834, 336.3622116, 324.793483, 315.4606934)), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
So far I have tried this:
plot_fig4 <- ggplot()+
geom_line(data=fig4, aes(x=dias, y=complete_preds_means), colour="blue")+
geom_line(data=fig4, aes(x=dias, y=contrafact), colour="red") +
geom_ribbon(aes(ymin=fig4$complete_preds_lower, ymax=fig4$complete_preds_upper))+
labs(y="Clase ($)",
x="") +
scale_y_continuous(breaks=seq(from=100, to=800, by=100))+
scale_x_date(expand = c(0, 0), date_breaks="1 month", date_labels = "%b\n%Y")
But I only get this error: Error: geom_ribbon requires the following missing aesthetics: x or y, xmin and xmax
You haven't told geom_ribbon what variable should be on the x axis. You could just add x=fig4$dias inside the aes of geom_ribbon, but this isn't the best way to use ggplot. Better to use ggplot's inheritance of data and aesthetic mappings to avoid repeating yourself and making mistakes along the way. If you change your first line to ggplot(fig4, aes(x = dias)) you don't need to do data=fig4 and x=dias in every geom call.
A couple of other issues are that you should map the color aesthetic to produce a legend, and make the alpha low on your ribbon so that it is semi-transparent. The ordering of layers is also important.
Finally, I have added some theme tweaks to make the plot more like the desired output.
ggplot(fig4, aes(x = dias)) +
geom_line(aes(y = contrafact, color = "Contrafact"), linewidth = 1) +
geom_ribbon(aes(ymin = complete_preds_lower, ymax = complete_preds_upper),
fill = "deepskyblue4", alpha = 0.2) +
geom_line(aes(y = complete_preds_means, color = "Predicted"), linewidth = 1) +
geom_vline(xintercept = as.Date("2022-03-13"),
linetype = 4, colour = "green4") +
labs(y = "Clase ($)", x = NULL) +
scale_color_manual(NULL, values = c("orange", "deepskyblue4")) +
scale_y_continuous(breaks = 1:8 * 100) +
scale_x_date(expand = c(0, 0), date_breaks = "1 month",
date_labels = "%b\n%Y") +
theme_classic(base_size = 16)

How to set facet plot y axis limits for each column

I have a sample dataset as below:
Day<-c("1","1","1","2","2","2")
Group<-c("Blue","Red","Green","Blue","Red","Green")
UV<-c("3","4","2","5","4","6")
Rain<-c("10","11","12","15","16","17")
dmean<-data.frame(Day,Group,UV,Rain)
Day<-c("1","1","1","1","1","1","2","2","2","2","2","2")
Group<-c("Blue","Blue","Red","Red","Green","Green","Blue","Blue","Red","Red","Green","Green")
UV<-c("3","3.1","4","4.1","2","2.2","5","5.1","4","4.2","6.1","6.1")
Rain<-c("10","10.1","11","11","12","12.2","15","15.2","16","16.1","17","17.2")
dpoints<-data.frame(Day,Group,UV,Rain)
library(ggplot2)
plot.ts <- function(yvar) {
ggplot(dmean, aes(x = Day, y = .data[[yvar]], group = Group, colour = Group)) +
geom_line(size = 1)+
geom_point(data = dpoints, aes(y = .data[[yvar]]), alpha = .2) +
facet_wrap(~Group, ncol = 1)
}
lapply(names(dpoints)[3:4], plot.ts)
Is it possible to modify the function "plot.ts" so I can set y axis limits for each "yvar" column, in this case "UV" and "Rain"? I have a larger dataset where there are more columns besides "UV" and "Rain" to apply the "plot.ts" function on.
Thank you.
We can use the {facetscales} package. We need to create a list with each column name and can give each column name its own scale_ function. If you want to pass each iteration its own list, then we have to create a list of lists and use mapply or purrr::map2.
library(ggplot2)
library(facetscales)
plot.ts <- function(yvar, ylimits = NULL) {
p <- ggplot(dmean, aes(x = Day, y = .data[[yvar]], group = Group, colour = Group)) +
geom_line(size = 1)+
geom_point(data = dpoints, aes(y = .data[[yvar]]), alpha = .2)
if(!is.null(ylimits)) {
p + facet_grid_sc(rows = vars(Group), scales = list(y = ylimits))
} else
p + facet_grid(rows = vars(Group))
}
scales_y <- list(
Blue = scale_y_continuous(limits = c(0,50)),
Green = scale_y_continuous(limits = c(12,14)),
Red = scale_y_continuous(limits = c(10,25))
)
lapply(names(dpoints)[3:4], plot.ts, ylimits = scales_y)
#> [[1]]
#>
#> [[2]]
Created on 2022-05-02 by the reprex package (v0.3.0)
Below the slightly altered data:
Day<-c("1","1","1","2","2","2")
Group<-c("Blue","Red","Green","Blue","Red","Green")
UV<-c("3","4","2","5","4","6")
Rain<-c("10","11","12","15","16","17")
dmean<-data.frame(Day,Group,UV,Rain)
dmean$UV <- as.numeric(dmean$UV)
dmean$Rain <- as.numeric(dmean$Rain)
Day<-c("1","1","1","1","1","1","2","2","2","2","2","2")
Group<-c("Blue","Blue","Red","Red","Green","Green","Blue","Blue","Red","Red","Green","Green")
UV<-c("3","3.1","4","4.1","2","2.2","5","5.1","4","4.2","6.1","6.1")
Rain<-c("10","10.1","11","11","12","12.2","15","15.2","16","16.1","17","17.2")
dpoints<-data.frame(Day,Group,UV,Rain)
dpoints$UV <- as.numeric(dmean$UV)
dpoints$Rain <- as.numeric(dmean$Rain)

Fail to set the color and dot size separately when using ggplot with two datasets

I have a data set as shown here:
ALL<- structure(list(GI = c(38.448275862069, 40.2659574468085, 85.3378378378378,
56.4606741573034, 26.5714285714286, 16.8944099378882), GI_D = c(31.5275862068966,
37.0446808510638, 64.0033783783784, 45.7331460674157, 20.7257142857143,
14.1913043478261), GI_W = c(34.84375, 39.4270833333333, 83.0921052631579,
54.6195652173913, 25.5963302752294, 16.4848484848485), NEE_D_mean = c(9.9644036070938,
-5.49181483024952, -29.5841687938457, -10.950117466455, -9.76133775037159,
-1.17370950853892), NEE_D_se = c(24.4055666454516, 8.31286897717958,
43.0803839446216, 42.0054504158082, 28.7765100449838, 8.86774764999355
), NEE_W_mean = c(-10.6866769282934, 20.9456806199394, -24.0380682586804,
52.3723812566745, -62.2858574112861, 56.3557615426375), NEE_W_se = c(15.2426118086142,
17.8227858145903, 22.7452815581715, 38.4251278858896, 19.1950340008666,
25.59062272811), GPP_D_mean = c(2.76586256588453, -14.0740484535984,
22.0551675189495, 38.2196758481854, -22.2452106112792, 2.92247497333855
), GPP_D_se = c(10.0301104827162, 4.76830515667558, 10.1200654792974,
13.6220945562145, 12.5521089272372, 4.02070599220442), GPP_W_mean = c(-13.3583364224079,
5.5457128851295, 6.96224944388818, 30.9347346550519, -24.0637392356731,
31.1919112040759), GPP_W_se = c(7.79177565854901, 7.68225824264646,
7.53759987843893, 9.21062180693269, 11.5998936888688, 4.91032534186175
), RE_D_mean = c(-6.92656657644594, -20.2249090077204, -1.55891573291113,
15.3619823271736, -59.6169736724781, 0.0398744940922411), RE_D_se = c(8.81296607135718,
3.17951327169943, 7.26103092218914, 9.79375075847273, 33.89046634443,
3.15632251128507), RE_W_mean = c(-11.2826765406364, -5.50930629197934,
-7.35527862198859, -3.3802491396303, -5.7039196948544, 15.5927675710877
), RE_W_se = c(7.82782177993256, 3.28089787167971, 5.27000717925753,
5.7667863399033, 10.1830962186111, 3.17699751136105), site = c("DK_M",
"DK_B", "UK", "NL", "HU", "IT")), row.names = c(NA, -6L), class = "data.frame")
And now I want to make a plot similar to below,
My code is
library(dplyr)
require(ggplot2)
require(ggpmisc)
library(tidyr)
library(tidyverse)
target1<- c("UK", "DK_M", "NL","DK_B") #What about "DK_B"?
dat<- filter(ALL, site %in% target1)
fit<- lm(NEE_D_mean~GI,dat)
summary(fit)
target2<- c("HU", "DK_M","NL","DK_B")
df<- filter(ALL, site %in% target2)
fit<- lm(RE_D_mean~GI,df)
summary(fit)
ggplot(ALL, mapping = aes(x=GI, y=NEE_D_mean))+
geom_point(aes(x=GI, y=NEE_D_mean,shape=site,color= 'green', size=1))+
geom_hline(yintercept = 0)+ #add a horizontal line= 0
geom_errorbar(aes(ymin=NEE_D_mean-NEE_D_se, ymax=NEE_D_mean+NEE_D_se), width=0.5) +
labs(y='Drought change of NEE from control % ', x= 'Gaussen Index of Aridity', color= ' ')+ #here, note: x and y axis title is reversed.
geom_smooth (data = dat,aes(x=GI, y=NEE_D_mean),method='lm', formula = y~x,color= 'black', se=FALSE,inherit.aes = FALSE) +
#stat_poly_eq(formula = y~x, eq.with.lhs = "italic(hat(y))~`=`~", aes(x = 65, y = -20,label = paste(..eq.label.., ..rr.label.., sep = "~~~")), parse = TRUE) +
geom_point(aes(x=GI, y=RE_D_mean,shape= site,color= "blue",size=2))+ #if I add color= "bule" here, it doesn't work at all. why?
geom_hline(yintercept = 0)+ #add a horizontal line= 0
geom_errorbar(aes(ymin=RE_D_mean-RE_D_se, ymax=RE_D_mean+RE_D_se, color= "blue"), width=0.5, size=1) + #if I add color= "blue" here, it doesn't work at all. why?
labs(y='Drought change of Reco from control % ', x= 'Gaussen Index of Aridity', color= ' ')+
scale_color_manual(values = c("NEE"="black", "RE"="green"), drop= F)+ #change the color and match the color with the second legend
geom_smooth (data = df,aes(x=GI, y=RE_D_mean),method='lm', formula = y~x,color= 'green', se=FALSE,inherit.aes = FALSE) +
theme_bw()+
#theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank())+
theme(legend.position = c(0.85, 0.3))+ #change the legend position
theme(legend.title = element_blank())+ #Change the legend title to blank
theme_bw()+
theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank())
#+annotate(geom="text", x=60, y=10, label="NEE",color="red")
The problem is I can't change the size and color of the dots separately. I had set the size of the dots separately, it showed a weird legend on the left. Meanwhile, even if I set the color of the second (RE) graph's dots and error bars to blue, the output color didn't change.

how to return the expected graph (ggplot2, ellipse in r)?

Hy, I have this:
library(ggplot2)
data.frame(obs=c("X1","X2","X3","X4","X5","X6","X7","X8","X9","X10","X11","X12","X13","X14","X15","X16","X17","X18","X19","X20","X21","X22","X23","X24"),
V1=c(0.12299204,0.82459186,0.74426426,0.74518490,0.90235626,0.80238518,0.14670287,0.29485058,0.15263963,0.35564952,0.74018218,0.90043726,0.89870240,0.11331604,0.38224485,0.30905100,0.16173775,0.06165922,0.16236031,0.19410052,0.15621499,0.14355361,0.09841802,0.14200906),
V2=c(-0.07667381, 0.05661226,-0.06755684, 0.24279246, 0.20566121, 0.09090093,-0.15771084, 0.68288156, 0.05995853, 0.17265401, 0.10524241, 0.28202393, 0.13269340,-0.06152291, 0.14251037, 0.32915325,-0.15536643,-0.06455349, 0.89639327, 0.25273722,-0.12570665, 0.95207149, 0.32158002, 0.93175509),
V3=c( 0.785128638, 0.137638800, 0.413372884, 0.071252925, 0.153475018, 0.288985725,-0.003831879,-0.035310199, 0.149559691, 0.180806799,-0.011326334,-0.029067359, 0.093260484,-0.053193781,-0.068764135, 0.123221407,-0.044328208, 0.857713525, 0.104352647, 0.807427676, 0.504358226,-0.013406375, 0.751067158, 0.105874634),
V4=c(-0.017742238, 0.216325083, 0.121780449, 0.114055328, 0.078455652, 0.218005225, 0.405876010,-0.089009642, 0.099241491, 0.107780652, 0.248500052, 0.050069182, 0.219363523,-0.094918144, 0.725727266, 0.724975094, 0.621679968,-0.118206228, 0.145886691, 0.249329008,-0.188579718, 0.049934405, 0.007372841, 0.025672350),
V5=c( 0.01547677, 0.16555178, 0.26019454, 0.08977182, 0.21660527, 0.35163419, 0.71035498, 0.35886762, 0.87849390,-0.01351151,-0.35429053, 0.02127159,-0.05830117,-0.05454903, 0.06498632, 0.06387208, 0.21214453,-0.07420916,-0.11399133, 0.13361320, 0.17796307,-0.01886769, 0.16513371,-0.02091601),
V6=c( 0.071459381, 0.103824145, 0.296293694, 0.318562131, 0.026584128, 0.044575715, 0.394070695, 0.301593577,-0.070145516, 0.815826736, 0.204876351,-0.006624310, 0.087139899,-0.008305995, 0.088330589, 0.229090697,-0.256193405,-0.068229882, 0.059314345, 0.163380601, 0.603980086,-0.020015746, 0.219288132,-0.040558617),
V7=c( 0.0937721448, 0.1257961637, 0.0818954368, 0.3470549669,-0.0145647326, 0.0595075312,-0.0075184176,-0.1745364284,-0.0544797910, 0.0934216940,-0.0933605187,-0.1502702725, 0.0734903991, 0.9511026556,-0.1550272896,-0.0449080619, 0.0524177580,-0.1177789182, 0.0040840036,-0.0208707253,-0.1842557001, 0.0124933572,-0.0008850238,-0.0037866599),
group=c("I","I","II","II","II","II","III","III","III","II","II","II","II","III","III","III","III","II","II","II","II","III","III","III")
)->df
ggplot(y,aes(x = V1, y = V2,label=factor(obs), color=factor(group), shape =factor(group))) +
geom_text_repel(show_guide=F)+
geom_point(size=2)+
guides(
colour = guide_legend(
override.aes = list(shape = c(15,16,17))
)
)+
scale_shape(guide = FALSE)+
labs(color="Legend")+
guides(
shape = FALSE,
color = guide_legend(override.aes = list(shape = c(16,17,15))))+
theme_classic()
Output:
I couldn't integrate the stat_ellipse function, any idea how to make the graph below?
Expected output:
You need a variable/criteria for each ellipse, here an example for two random ellipses.
Data
ellipse1 <- c("X23","X20","X16","X10","X15","X18","X14","X1","X21","X7")
df <-
data.frame(
obs=c("X1","X2","X3","X4","X5","X6","X7","X8","X9","X10","X11","X12","X13","X14","X15","X16","X17","X18","X19","X20","X21","X22","X23","X24"),
V1=c(0.12299204,0.82459186,0.74426426,0.74518490,0.90235626,0.80238518,0.14670287,0.29485058,0.15263963,0.35564952,0.74018218,0.90043726,0.89870240,0.11331604,0.38224485,0.30905100,0.16173775,0.06165922,0.16236031,0.19410052,0.15621499,0.14355361,0.09841802,0.14200906),
V2=c(-0.07667381, 0.05661226,-0.06755684, 0.24279246, 0.20566121, 0.09090093,-0.15771084, 0.68288156, 0.05995853, 0.17265401, 0.10524241, 0.28202393, 0.13269340,-0.06152291, 0.14251037, 0.32915325,-0.15536643,-0.06455349, 0.89639327, 0.25273722,-0.12570665, 0.95207149, 0.32158002, 0.93175509),
group=c("I","I","II","II","II","II","III","III","III","II","II","II","II","III","III","III","III","II","II","II","II","III","III","III")
) %>%
as_tibble() %>%
mutate(
ellipse = case_when(
obs %in% ellipse1 ~ 1,
TRUE ~ 2
)
)
How to
df %>%
ggplot(aes(x = V1, y = V2,label=factor(obs), color=factor(group), shape =factor(group))) +
geom_text_repel(show.legend = FALSE)+
geom_point(size=2)+
guides(
shape = FALSE,
colour = guide_legend(
override.aes = list(shape = c(16,17,15))
)
)+
labs(color="Legend")+
theme_classic()+
stat_ellipse(aes(group = ellipse), linetype = "dashed")

Resources