If this question has already been answered, please link as I have not been able to locate a similar question. I have referred to R bar plot with 3 variables, Bar plot with multiple variables in R, ggplot with 2 y axes on each side and different scales,Bar Plot with 2 y axes and same x axis in R language [duplicate],Bar Plot with 2 Y axes and same X- axis.
I have a dataset that includes species, observed value, expected value, and a standardized value from the observed and expected.
data <- structure(list(Species = c("BABO_BW", "BABO_BW", "BABO_BW", "BABO_RC",
"BABO_RC", "BABO_RC", "BABO_SKS", "BABO_SKS", "BABO_SKS", "BABO_MANG",
"BABO_MANG", "BABO_MANG", "BW_RC", "BW_RC", "BW_RC", "BW_SKS",
"BW_SKS", "BW_SKS", "BW_MANG", "BW_MANG", "BW_MANG", "RC_SKS",
"RC_SKS", "RC_SKS", "RC_MANG", "RC_MANG", "RC_MANG", "SKS_MANG",
"SKS_MANG", "SKS_MANG"), variable = c("obs.C-score", "exp.C-score",
"SES_Cscore", "obs.C-score", "exp.C-score", "SES_Cscore", "obs.C-score",
"exp.C-score", "SES_Cscore", "obs.C-score", "exp.C-score", "SES_Cscore",
"obs.C-score", "exp.C-score", "SES_Cscore", "obs.C-score", "exp.C-score",
"SES_Cscore", "obs.C-score", "exp.C-score", "SES_Cscore", "obs.C-score",
"exp.C-score", "SES_Cscore", "obs.C-score", "exp.C-score", "SES_Cscore",
"obs.C-score", "exp.C-score", "SES_Cscore"), value = c(328680,
276507, 6.73358774036271, 408360, 345488, 5.31345024375997, 285090,
254670, 4.35376633657727, 12474, 12190, 1.24624427424057, 1450800,
1809738, -11.0195450589776, 1507488, 1361088, 6.15672144449049,
62706, 65780, -0.495728742814285, 1790156, 1700165, 2.70409191051284,
45701, 86301, -4.71151949799025, 42240, 62745, -4.52203636797869
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-30L))
Sample Output
Species Variable Value
1 BABO_BW obs.C-score 328680.0000000
2 BABO_BW exp.C-score 276507.0000000
3 BABO_BW SES_Cscore 6.7335877
4 BABO_MANG obs.C-score 12474.0000000
5 BABO_MANG exp.C-score 12190.0000000
6 BABO_MANG SES_Cscore 1.2462443
7 BABO_RC obs.C-score 408360.0000000
8 BABO_RC exp.C-score 345488.0000000
9 BABO_RC SES_Cscore 5.3134502
10 BABO_SKS obs.C-score 285090.0000000
I am trying to put the SES_Cscore on the x-axis and have the obs.C-score and exp.C-score as bars. The species column groupings the C-scores, so I would like to include those in the x-axis as well.
I have been able to plot the species at the y and the other variables as bar graphs.
ggplot(data,aes(x = Species,y = value)) +
geom_bar(aes(fill = variable),stat = "identity",position = "dodge")
I would like to have the continuous variable of SES_Cscore as well on the x-axis. Is there a way to do this?
Thank you in advance and have a lovely day!