Related
I would like to plot both the raw data from df_plotting and the summary statistics from table_max (the mean values per conditon).
This is my code so far
Edit: Add apa <- c("#68246d", "#a53d55", "#b87246", "#afa961")
ggplot(table_max, mapping= aes(x = phase_bins, y = Mean)) +
geom_line(aes(group=as.numeric(cond_f),
colour = cond_f), size = 1.2)+
geom_line(df_plotting, mapping = aes(x=phase_bins, y = max_change_to_base,
group=interaction(ID, cond_f),
colour = cond_f, linetype =cond_f),
alpha = 0.3, size =0.3)+
geom_errorbar(table_max, mapping=aes(ymin=Mean-SD, ymax=Mean+SD, colour = cond_f),
width=.2, size = 0.7)+
facet_wrap(factor(sample, levels = c("UGi", "UKi", "UKa"))~.)+
labs(title="Max. temperature change by condition and sample", colour = "Condition:", linetype = "Condition:")+
theme(guides(linetype=guide_legend(override.aes = list(alpha = 1)))+
scale_x_discrete(labels=
c("base", "stim1", "stim2", "recovery", "break"), drop = T)+
scale_y_continuous(limits = c(-2,3))+
scale_colour_manual(name = "Condition:",
labels = c("artificial", "cry", "laugh", "babble"), values=apa) +
scale_linetype_manual(name = "Condition:",
labels = c("artificial", "cry", "laugh", "babble"),
values = c(1,2,3,4)) # also tried "solid", "dotted", "dashed", "twodashed"
The problem is, that this displays only solid lines for the legend, and I would like a combination of linetype and colour as one joint legend. I think this does not work because colour is assigned once to solid lines and then to the combination of linetype and colour and I can't override only one of the colour aesthetics.
Here is my data:
df_plotting
structure(list(ID = structure(c(35L, 35L, 35L, 35L, 35L), .Label = c("UG201",
"UG208", "UG209", "UG211", "UG215", "UG217", "UG219", "UG220",
"UG221", "UG222", "UG228", "UG243", "UG247", "UG254", "UG268",
"UG271", "UG272", "UG273", "UG274", "UG275", "UG280", "UG283",
"UG284", "UG286", "UG297", "UG299", "UG308", "UG310", "UG315",
"UG316", "UG330", "UG331", "UG334", "UG335", "UK103", "UK104",
"UK105", "UK106", "UK107", "UK108", "UK110", "UK111", "UK112",
"UK113", "UK115", "UK116", "UK117", "UK119", "UK122", "UK123",
"UK130", "UK132", "UK135", "UK136", "UK138", "UK139", "UK140",
"UK142", "UK145", "UK147", "UK150", "UK153", "UK155", "UK156",
"UK159", "UK160", "UK162", "UK164", "UKA102", "UKA103", "UKA104",
"UKA105", "UKA106", "UKA107", "UKA108", "UKA109", "UKA110", "UKA111",
"UKA112", "UKA113", "UKA114", "UKA115", "UKA116", "UKA117", "UKA119",
"UKA120", "UKA121", "UKA122"), class = "factor"), sex = structure(c(1L,
1L, 1L, 1L, 1L), .Label = c("f", "m"), class = "factor"), trial = structure(c(1L,
1L, 1L, 1L, 2L), .Label = c("1", "2", "3", "4"), class = "factor"),
cond_f = structure(c(3L, 3L, 3L, 3L, 2L), .Label = c("artificial",
"cry", "laugh", "babble"), class = "factor"), stimulus = structure(c(16L,
16L, 16L, 16L, 12L), .Label = c("a1", "a2", "a3", "a4", "b1",
"b2", "b3", "b4", "c1", "c2", "c3", "c4", "l1", "l2", "l3",
"l4"), class = "factor"), phase_bins = structure(c(2L, 3L,
4L, 5L, 2L), .Label = c("pre", "baseline", "stim_bin1", "stim_bin2",
"recovery", "break"), class = "factor"), mean_change_to_base = c(0,
-0.516666666666667, -0.0333333333333336, 0.433333333333333,
0), max_change_to_base = c(0, -0.933333333333334, 0.166666666666668,
0.566666666666666, 0), sample = structure(c(2L, 2L, 2L, 2L,
2L), .Label = c("UGi", "UKi", "UKa"), class = "factor")), row.names = c(NA,
-5L), groups = structure(list(ID = structure(c(35L, 35L), .Label = c("UG201",
"UG208", "UG209", "UG211", "UG215", "UG217", "UG219", "UG220",
"UG221", "UG222", "UG228", "UG243", "UG247", "UG254", "UG268",
"UG271", "UG272", "UG273", "UG274", "UG275", "UG280", "UG283",
"UG284", "UG286", "UG297", "UG299", "UG308", "UG310", "UG315",
"UG316", "UG330", "UG331", "UG334", "UG335", "UK103", "UK104",
"UK105", "UK106", "UK107", "UK108", "UK110", "UK111", "UK112",
"UK113", "UK115", "UK116", "UK117", "UK119", "UK122", "UK123",
"UK130", "UK132", "UK135", "UK136", "UK138", "UK139", "UK140",
"UK142", "UK145", "UK147", "UK150", "UK153", "UK155", "UK156",
"UK159", "UK160", "UK162", "UK164", "UKA102", "UKA103", "UKA104",
"UKA105", "UKA106", "UKA107", "UKA108", "UKA109", "UKA110", "UKA111",
"UKA112", "UKA113", "UKA114", "UKA115", "UKA116", "UKA117", "UKA119",
"UKA120", "UKA121", "UKA122"), class = "factor"), trial = structure(1:2, .Label = c("1",
"2", "3", "4"), class = "factor"), cond_f = structure(3:2, .Label = c("artificial",
"cry", "laugh", "babble"), class = "factor"), .rows = structure(list(
1:4, 5L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
table_max
structure(list(cond_f = structure(c(1L, 1L, 1L, 1L, 1L), .Label = c("artificial",
"cry", "laugh", "babble"), class = "factor"), phase_bins = structure(c(2L,
2L, 2L, 3L, 3L), .Label = c("pre", "baseline", "stim_bin1", "stim_bin2",
"recovery", "break"), class = "factor"), sample = structure(c(1L,
2L, 3L, 1L, 2L), .Label = c("UGi", "UKi", "UKa"), class = "factor"),
Mean = c(0, 0, 0, 0.244444444444444, 0.711111111111109),
SD = c(0, 0, 0, 0.760260897979524, 0.474268442504406), Min. = c(0,
0, 0, -0.899999999999999, 0.133333333333333), Max. = c(0,
0, 0, 1.8, 1.4), count = c(12L, 9L, 20L, 12L, 9L)), row.names = c(NA,
-5L), groups = structure(list(cond_f = structure(c(1L, 1L), .Label = c("artificial",
"cry", "laugh", "babble"), class = "factor"), phase_bins = structure(2:3, .Label = c("pre",
"baseline", "stim_bin1", "stim_bin2", "recovery", "break"), class = "factor"),
.rows = structure(list(1:3, 4:5), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
Thanks for any ideas on how to fix this!
The problem was in cond_f's factor levels. Note that I define both the vector of colors apa and the vector of line types lty. And assign names to each of these colors and line types from the factor cond_f levels.
First the plot without the scale_*'s.
library(ggplot2)
apa <- c("#68246d", "#a53d55", "#b87246", "#afa961")
apa <- setNames(apa, levels(df_plotting$cond_f))
lty <- c("solid", "dotted", "dashed", "twodash")
lty <- setNames(lty, levels(df_plotting$cond_f))
p <- ggplot(table_max, mapping= aes(x = phase_bins, y = Mean,
group = cond_f, colour = cond_f,
linetype = cond_f)) +
geom_line(size = 1.2) +
geom_line(
data = df_plotting,
mapping = aes(x = phase_bins, y = max_change_to_base,
group = interaction(ID, cond_f),
colour = cond_f,
linetype = cond_f),
alpha = 0.3, size = 0.3,
inherit.aes = FALSE
) +
geom_errorbar(
mapping = aes(ymin = Mean - SD, ymax = Mean + SD),
width = 0.2,
size = 0.7
)
Created on 2022-07-25 by the reprex package (v2.0.1)
Now the problem. The default drop = TRUE drops the unused factor levels, set it to FALSE on both scale_colour_manual and scale_linetype_manual.
p2 <- p +
scale_x_discrete(
labels = c("base", "stim1", "stim2", "recovery", "break"),
drop = TRUE
) +
scale_y_continuous(limits = c(-2, 3)) +
scale_colour_manual(
name = "Condition:",
labels = c("artificial", "cry", "laugh", "babble"),
values = apa,
drop = FALSE
) +
scale_linetype_manual(
name = "Condition:",
labels = c("artificial", "cry", "laugh", "babble"),
values = lty,
drop = FALSE
) +
labs(
title = "Max. temperature change by condition and sample",
colour = "Condition:",
linetype = "Condition:"
) +
facet_wrap(sample ~ .)
p2
#> geom_path: Each group consists of only one observation. Do you need to adjust
#> the group aesthetic?
Created on 2022-07-25 by the reprex package (v2.0.1)
I don't believe this makes a difference.
p2 + guides(linetype = guide_legend(override.aes = list(alpha = 1)))
I would like create a plot like this:
i.e. i would like to add another bar plot inside in my bar plot basic,
whould could i do something like this ?
i have no idea about how create this,
data:
structure(list(Impacted_sector = structure(c(3L, 3L, 1L, 1L,
5L, 2L, 4L), .Label = c("Authorities-Stakeholders", "Public and social welfare",
"Agriculture", "Variety", "Environment"), class = "factor", scores = structure(c(Agriculture = -4.49129192,
`Authorities-Stakeholders` = -3125.027684115, Environment = -0.33176146,
`Public and social welfare` = -15.46511976, Variety = -0.39712811
), .Dim = 5L, .Dimnames = list(c("Agriculture", "Authorities-Stakeholders",
"Environment", "Public and social welfare", "Variety")))), Type_of_cost_merged = structure(c(2L,
1L, 1L, 3L, 1L, 2L, 1L), .Label = c("Management", "Damage", "Mixed"
), class = "factor", scores = structure(c(Damage = -7.803309445,
Management = -1564.7958562425, Mixed = -0.44191754), .Dim = 3L, .Dimnames = list(
c("Damage", "Management", "Mixed")))), cost = c(141499.13,
8841084.71, 6249613450.69, 441917.54, 331761.46, 15465119.76,
397128.11), Million = c(0.14149913, 8.84108471, 6249.61345069,
0.44191754, 0.33176146, 15.46511976, 0.39712811)), row.names = c(NA,
-7L), groups = structure(list(Impacted_sector = structure(1:5, .Label = c("Authorities-Stakeholders",
"Public and social welfare", "Agriculture", "Variety", "Environment"
), scores = structure(c(Agriculture = -4.49129192, `Authorities-Stakeholders` = -3125.027684115,
Environment = -0.33176146, `Public and social welfare` = -15.46511976,
Variety = -0.39712811), .Dim = 5L, .Dimnames = list(c("Agriculture",
"Authorities-Stakeholders", "Environment", "Public and social welfare",
"Variety"))), class = "factor"), .rows = structure(list(3:4,
6L, 1:2, 7L, 5L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
All help is appreciate
As I mentioned in my comment one option would be to make use of patchwork::inset_element. As in your data the cost for Authorities-Stakeholders differs heavily from the cost of the other categories I opted for adding Authorities-Stakeholders as an inset plot.
library(ggplot2)
library(patchwork)
library(dplyr)
p1 <- dd %>%
filter(!Impacted_sector == "Authorities-Stakeholders") %>%
ggplot(aes(Impacted_sector, cost, fill = Type_of_cost_merged)) +
geom_col() +
scale_y_continuous(labels = scales::number_format(scale = 1e-6), expand = c(0, .05)) +
theme_minimal() +
guides(fill = guide_legend(override.aes = list(color = "black"))) +
labs(fill = "Type of cost")
p2 <- dd %>%
filter(Impacted_sector == "Authorities-Stakeholders") %>%
ggplot(aes(Impacted_sector, cost, fill = Type_of_cost_merged)) +
geom_col() +
scale_y_continuous(labels = scales::number_format(scale = 1e-6), expand = c(0, .05)) +
theme_minimal() +
theme(plot.background = element_rect(fill = "white", color = NA)) +
guides(fill = "none") +
labs(x = NULL, y = NULL)
p1 + patchwork::inset_element(p2, .6, .6, 1, 1)
I have used ggplot2 to create a line graph for a soil water release curve. However, because I only have one data point at each pressure value (the x axis), the lines are connected directly from point to point. I would like to keep the points but have a curve that shows the trend of the points. This is the typical style for soil water release curves.
Data:
> dput(head(sub2018))
structure(list(Year = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label =
c("2018",
"2019"), class = "factor"), Pressure = structure(1:6, .Label = c("-1",
"-0.5", "-0.25", "-0.2", "-0.1", "-0.05", "-0.02", "-0.01", "0"
), class = "factor"), meanVWC = c(0.291819594, 0.308328767666667,
0.318496127666667, 0.323671866333333, 0.349356212666667,
0.374201803666667
)), row.names = c(NA, -6L), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), vars = "Year", drop = TRUE, indices = list(
0:5), group_sizes = 6L, biggest_group_size = 6L, labels = structure(list(
Year = structure(1L, .Label = c("2018", "2019"), class = "factor")),
row.names = c(NA,
-1L), class = "data.frame", vars = "Year", drop = TRUE))
ggplot:
GGplot2018 <- ggplot(sub2018, aes(x=Pressure, y=meanVWC, group=1)) +
geom_line() +
geom_point() + labs(y= "Volumetric Water Content")
GGplot2018
Does anyone know if/how I can add this curve?
Thanks very much for any help!
I believe this is what you are looking for:
GGplot2018 <- ggplot(sub2018, aes(x=Pressure, y=meanVWC, group=1)) +
geom_line()+
geom_point() + labs(y= "Volumetric Water Content")+
geom_smooth(method = "lm",se = FALSE)
GGplot2018
I have the following data frame:
structure(list(StepsGroup = structure(c(1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L), .Label = c("(-Inf,3e+03]", "(3e+03,1.2e+04]", "(1.2e+04, Inf]"
), class = "factor"), GlucoseGroup = structure(c(1L, 2L, 3L,
1L, 2L, 3L, 1L, 2L, 3L), .Label = c("<100", "100-180", ">180"
), class = "factor"), n = c(396L, 1600L, 229L, 787L, 4182L, 375L,
110L, 534L, 55L), freq = c(0.177977528089888, 0.719101123595506,
0.102921348314607, 0.147267964071856, 0.782559880239521, 0.0701721556886228,
0.157367668097282, 0.763948497854077, 0.0786838340486409)), class =
c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -9L), vars = "StepsGroup",
labels = structure(list(
StepsGroup = structure(1:3, .Label = c("(-Inf,3e+03]", "(3e+03,1.2e+04]",
"(1.2e+04, Inf]"), class = "factor")), class = "data.frame", row.names =
c(NA, -3L), vars = "StepsGroup", drop = TRUE), indices = list(0:2,
3:5, 6:8), drop = TRUE, group_sizes = c(3L, 3L, 3L), biggest_group_size =
3L)
I would like to create a stacked bar plot, and add a summary of each StepsGroup on top of each bar. So the first group will have 2225, the second 5344 and the third 699.
I am using the following script:
ggplot(d_stepsFastingSummary , aes(y = freq, x = StepsGroup, fill =
GlucoseGroup)) + geom_bar(stat = "identity") +
geom_text(aes(label = sum(n()), vjust = 0))
The part until before the geom_text works, but for the last bit I get the following error:
Error: This function should not be called directly
Any idea how to add the aggregated quantity?
We could create a new dataframe stacked_df which would have sum for each StepsGroup
stacked_df <- df %>% group_by(StepsGroup) %>% summarise(nsum = sum(n))
ggplot(df) +
geom_bar(aes(y = freq, x = StepsGroup, fill= GlucoseGroup),stat = "identity") +
geom_text(data = stacked_df, aes(label = nsum, StepsGroup,y = 1.1))
This might seem a really stupid mistake on my part but whenever I specify geom_point depending on a factor and choose a hollow point and a solid point (shapes 1 and 19), and plot error bars, it crosses the point.
Here are my data frames:
> dput(head(allbins.sum))
structure(list(T = c(0L, 0L, 10L, 10L, 20L, 20L), treatment = structure(c(1L,
2L, 1L, 2L, 1L, 2L), .Label = c("control bead", "dP bead"), class = "factor"),
N = c(3, 3, 3, 3, 3, 3), cellsBase = c(0, 0, 0.013028995209506,
0.135599858885737, -0.0130289952095061, 0.759359209760127
), sd = c(0, 0, 0.0597063567767786, 0.0469731690178533, 0.0983667566897066,
0.183436089048999), se = c(0, 0, 0.034471481157405, 0.0271199717771474,
0.0567920734541125, 0.105906875391532), ci = c(0, 0, 0.148318812500416,
0.116687820597672, 0.244356569875469, 0.455680506502609),
bin = c("BinA", "BinA", "BinA", "BinA", "BinA", "BinA")), .Names = c("T",
"treatment", "N", "cellsBase", "sd", "se", "ci", "bin"), row.names = c(NA,
6L), class = "data.frame")
> dput(head(allbins.fitdata))
structure(list(wellvidbin = structure(c(1L, 1L, 1L, 1L, 1L, 1L
), .Label = c("A1-002-BinA", "A1-002-BinB", "A1-002-BinC", "A1-031-BinA",
"A1-031-BinB", "A1-031-BinC", "A3-004-BinA", "A3-004-BinB", "A3-004-BinC",
"B1-032-BinA", "B1-032-BinB", "B1-032-BinC", "B4-026-BinA", "B4-026-BinB",
"B4-026-BinC", "C4-027-BinA", "C4-027-BinB", "C4-027-BinC"), class = "factor"),
treatment = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("control bead",
"dP bead"), class = "factor"), wellvid = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("A1-002", "A1-031", "A3-004",
"B1-032", "B4-026", "C4-027"), class = "factor"), bin = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("BinA", "BinB", "BinC"), class = "factor"),
T = c(0L, 10L, 20L, 30L, 40L, 50L), T.factor = structure(1:6, .Label = c("0",
"10", "20", "30", "40", "50", "60"), class = "factor"), cells = c(7L,
11L, 26L, 27L, 28L, 36L), cellsS = c(-1.36568429306349, -1.20296446240061,
-0.592765097414793, -0.552085139749072, -0.511405182083351,
-0.185965520757582), cellsBase = c(0, 0.162719830662884,
0.772919195648701, 0.813599153314422, 0.854279110980143,
1.17971877230591), treatT = structure(c(2L, 4L, 6L, 8L, 10L,
12L), .Label = c("control bead.0", "P bead.0", "control bead.10",
"P bead.10", "control bead.20", "P bead.20", "control bead.30",
"P bead.30", "control bead.40", "P bead.40", "control bead.50",
"P bead.50", "control bead.60", "P bead.60"), class = "factor"),
fit = c(0.0285939715820639, 0.304399288764407, 0.58020460594675,
0.856009923129092, 1.13181524031144, 1.40762055749378), se.fit = c(0.157415367032567,
0.132348142293459, 0.114707848741265, 0.108190467052118,
0.114707848741265, 0.132348142293459), upr = c(0.337128090965895,
0.563801647659587, 0.805031989479629, 1.06806323855124, 1.35664262384431,
1.66702291638896), lwr = c(-0.279940147801767, 0.0449969298692267,
0.35537722241387, 0.643956607706942, 0.906987856778556, 1.1482181985986
)), .Names = c("wellvidbin", "treatment", "wellvid", "bin",
"T", "T.factor", "cells", "cellsS", "cellsBase", "treatT", "fit",
"se.fit", "upr", "lwr"), class = c("data.table", "data.frame"
), row.names = c(NA, -6L), .internal.selfref = <pointer: 0x0000000000100788>)
And the code:
ggplot(data=allbins.sum, aes(x=T, y=cellsBase, shape=treatment)) + geom_point(size=5, aes(shape=treatment))+
geom_errorbar(aes(ymin=cellsBase-se, ymax=cellsBase+se), width=2, size=1) +
geom_smooth(data=allbins.fitdata, size=1, aes(y=fit, ymin=lwr, ymax=upr),
color="black", method="lm", stat="identity", alpha=0.2)+
facet_grid(bin~.) +
scale_shape_manual(values=c(1, 19))
This gives me this plot:
Any hints on how to have the hollow circles to be hollowed?
I also tried specifying geom_shape (aes(fill=treatment) and then scale_fill_manual but then it is also applied to my geom_smooth
Thanks for the help!
If you mean that you don't want the line of the error bar to be visible through the 'hollow' points, then plot geom_errorbar first, then plot geom_point second, with solid fill, so it will overlay the error bar.
ggplot(data=allbins.sum, aes(x=T, y=cellsBase)) +
# plotting this first
geom_errorbar(aes(ymin=cellsBase-se, ymax=cellsBase+se), width=2, size=1) +
# plotting this second, with a hollow fillable shape, and black outline
geom_point(size=5, shape = 21, color='black',
aes(fill = treatment)) +
# solid black and solid white fill for the points
scale_fill_manual(values = c('black', 'white')) +
theme_bw()
(The data you posted only has these points for allbins.sum, and the code for allbins.fitdata has an error, so no error bars on this plot)