Create a 180 degree rose diagram with differents colours for groups? - r

I am plotting a distribution of angles with the rose.diag function from the circular library. Input data are degrees.
circ <- circular(data2$mean_ang)
summary(circ)
rose.diag(circ, pch = 16, cex = 1, axes = TRUE, shrink = 1, col=3, prop = 2,
bins=36, upper=TRUE, ticks=TRUE, units="degrees")
I post a sample of data:
structure(list(sex = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L), .Label = c("F", "Fc", "M"), class = "factor"), area = structure(c(2L,
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("AA", "AM", "AR"
), class = "factor"), mean_ang = c(37.3785, 54.1439666666667,
58.26328, 26.0818202247191, 16.500981981982, 58.7045, 64.6254,
88.7488, 68.0051315789474, 50.7701449275362, 71.9524307692308,
29.7501111111111, 21.7672323943662, 14.6700987654321, 15.4569794238683,
12.7011125, 13.0968235294118, 28.6825, 12.7437857142857, 16.0024827586207,
21.531, 6.09045454545455, 7.09880503144654, 33.8071123595506,
40.1566071428571, 34.6079540983607, 18.1940236686391, 26.8186338028169,
27.2129230769231, 75.331826446281, 92.2394705882353, 38.6603613445378,
80.2414871794872, 68.7810454545454, 57.3119345238095, 99.8082886597938,
50.8413857142857, 16.519125, 52.6062, 79.46416875, 55.1253798882682,
41.7809574468085, 65.9881707317073, 56.9886991869919, 66.7067129186603,
81.9102918918919, 52.7566941747573, 75.806781512605, 52.1151818181818,
54.1975875, 65.5264748201439, 47.1095353535354, 47.7130379746835,
89.4254302325581, 32.5949724770642, 62.4567419354839, 65.1905301204819,
53.4842941176471, 9.7815641025641, 10.3269556650246, 36.6245238095238,
35.7347155963303, 40.1823980582524, 29.6765, 30.5416129032258,
20.003, 40.4984444444444, 82.9355, 35.5801836734694, 8.4906,
82.3376666666667, 68.5343045977012, 69.924, 76.5723333333333,
97.1923333333333, 32.8840909090909, 50.603, 31.5014230769231,
42.2313333333333, 27.7946888888889, 53.2960545454545, 48.8556814814815,
40.6237714285714, 67.7999126984127, 66.855390625, 99.226275862069,
102.765611111111, 53.9172142857143, 66.6297692307692, 73.7972580645161
)), row.names = c(NA, -90L), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), vars = c("sex", "area"), drop = TRUE, indices = list(
0:4, 5:12, 13:27, 28:57, 58:65, 66:89), group_sizes = c(5L,
8L, 15L, 30L, 8L, 24L), biggest_group_size = 30L, labels = structure(list(
sex = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("F",
"Fc", "M"), class = "factor"), area = structure(c(2L, 3L,
2L, 3L, 2L, 3L), .Label = c("AA", "AM", "AR"), class = "factor")), row.names = c(NA,
-6L), class = "data.frame", vars = c("sex", "area"), drop = TRUE))
Apparently, the diagram does not match the results found.
I turned all angles positive, previously..
I would like to build a diagram (if possible with ggplot) that shows the frequency density of the angles from 0 to 180 degrees.
The calculated maximum angle was 102.77 degrees.
In addition, I would like the colors to differ for the different sexes analyzed and areas.
Something like this:

Although you can do a polar chart in ggplot, you can't easily do a 180-degree polar plot. It is possible only with a bit of hacking. In this example, I have made a full polar plot, shifted it down the page, removed the gridlines, drawn in new gridlines, and created the "slices" from a stacked polar histogram.
The code isn't pretty, but the end result is quite nice.
library(ggplot2)
ggplot() +
geom_line(aes(x = c(0, 180), y = c(4, 4)), colour = "gray75") +
geom_line(aes(x = c(0, 180), y = c(8, 8)), colour = "gray75") +
geom_line(aes(x = c(0, 180), y = c(12, 12)), colour = "gray75") +
geom_line(aes(x = c(0, 180), y = c(16, 16)), colour = "gray75") +
geom_line(aes(x = c(0, 180), y = c(20, 20)), colour = "gray75") +
geom_vline(aes(xintercept = 0:6 * 30), colour = "gray75") +
geom_histogram(data = data2, aes(x = mean_ang, fill = sex),
position = "stack", colour = "black", binwidth = 15,
boundary = 0) +
coord_polar(start = 3 * pi / 2) +
scale_x_continuous(limits = c(0, 360), breaks = 0:6 * 30) +
scale_y_continuous(limits = c(0, 20)) +
theme_bw() +
theme(panel.border = element_blank(),
legend.margin = margin(unit(c(0, 5.5, 100, 5.5), "pt")),
axis.title.y = element_text(hjust = 0.75),
axis.title.x = element_text(vjust = 5),
plot.margin = margin(unit(c(50, 5.5, -100, 5.5), "pt")),
panel.grid = element_blank()) +
labs(title = "Mean degrees by sex", y = "Count")
Created on 2020-05-12 by the reprex package (v0.3.0)

Related

Change order in which ggplot plots the Y axis variables [duplicate]

My colleague and I are trying to order a stacked bar graph based on the y-values instead of alphabetically by the x-values.
The sample data is:
library(ggplot2)
samp.data <- structure(list(fullname = c("LJ", "PR",
"JB", "AA", "NS",
"MJ", "FT", "DA", "DR",
"AB", "BA", "RJ", "BA2",
"AR", "GG", "RA", "DK",
"DA2", "BJ2", "BK", "HN",
"WA2", "AE2", "JJ2"), I = c(2L,
1L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L), S = c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 3L, 3L,
3L), D = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 2L, 3L, 3L), C = c(0L, 2L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
2L, 3L, 3L, 3L, 3L)), .Names = c("fullname", "I", "S", "D", "C"
), class = "data.frame", row.names = c(NA, 24L))
md <- reshape2::melt(samp.data, id = (c("fullname")))
ggplot(data = md, aes(x = fullname, y = value, fill = variable)) +
geom_col()
But I ultimately want to sort by the sum of the 4 variables (I, S, D, and C) instead of the alphabetical order of the fullnames.
The general (non ggplot-specific) answer is to use reorder() to reset the factor levels in a categorical column, based on some function of the other columns.
## Examine the default factor order
levels(samp.data$fullname)
## Reorder fullname based on the the sum of the other columns
samp.data$fullname <- reorder(samp.data$fullname, rowSums(samp.data[-1]))
## Examine the new factor order
levels(samp.data$fullname)
attributes(samp.data$fullname)
Then just replot, using code from the original question
md <- melt(samp.data, id=(c("fullname")))
temp.plot<-ggplot(data=md, aes(x=fullname, y=value, fill=variable) ) +
geom_bar()+
theme(axis.text.x=theme_text(angle=90)) +
labs(title = "Score Distribtion")
## ggsave(temp.plot,filename="test.png")
A much simpler solution is to change the underlying function in reorder:
ggplot(data = md, aes(x = reorder(fullname, value, sum), y = value, fill = variable)) +
geom_col()

Reorder bars with percentages using ggplot2 [duplicate]

My colleague and I are trying to order a stacked bar graph based on the y-values instead of alphabetically by the x-values.
The sample data is:
library(ggplot2)
samp.data <- structure(list(fullname = c("LJ", "PR",
"JB", "AA", "NS",
"MJ", "FT", "DA", "DR",
"AB", "BA", "RJ", "BA2",
"AR", "GG", "RA", "DK",
"DA2", "BJ2", "BK", "HN",
"WA2", "AE2", "JJ2"), I = c(2L,
1L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L), S = c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 3L, 3L,
3L), D = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 2L, 3L, 3L), C = c(0L, 2L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
2L, 3L, 3L, 3L, 3L)), .Names = c("fullname", "I", "S", "D", "C"
), class = "data.frame", row.names = c(NA, 24L))
md <- reshape2::melt(samp.data, id = (c("fullname")))
ggplot(data = md, aes(x = fullname, y = value, fill = variable)) +
geom_col()
But I ultimately want to sort by the sum of the 4 variables (I, S, D, and C) instead of the alphabetical order of the fullnames.
The general (non ggplot-specific) answer is to use reorder() to reset the factor levels in a categorical column, based on some function of the other columns.
## Examine the default factor order
levels(samp.data$fullname)
## Reorder fullname based on the the sum of the other columns
samp.data$fullname <- reorder(samp.data$fullname, rowSums(samp.data[-1]))
## Examine the new factor order
levels(samp.data$fullname)
attributes(samp.data$fullname)
Then just replot, using code from the original question
md <- melt(samp.data, id=(c("fullname")))
temp.plot<-ggplot(data=md, aes(x=fullname, y=value, fill=variable) ) +
geom_bar()+
theme(axis.text.x=theme_text(angle=90)) +
labs(title = "Score Distribtion")
## ggsave(temp.plot,filename="test.png")
A much simpler solution is to change the underlying function in reorder:
ggplot(data = md, aes(x = reorder(fullname, value, sum), y = value, fill = variable)) +
geom_col()

Increase space between x-axis factors in ggplot

I am using the geom_pointrange function in ggplot2 in order to plot the spread of some measurement over different condition for 5 subjects. In order not to have the subjects overlap I have constructed the plot as follows:
Final = structure(list(Subject = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L), .Label = c("1", "2", "3", "4", "5"), class = "factor"),
X00.conditionName = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L,
4L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L,
4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 1L, 1L, 2L, 2L, 3L,
3L, 4L, 4L), .Label = c("EyeClose-Haptic", "mixed-Haptic_Visual",
"only-Haptic", "only-Visual"), class = "factor"), X03.totalTargetNumber = c(2L,
3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L,
2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L,
3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L), Accuracy = c(0.075870763,
0.0907863686, 0.0222156611, 0.0492028585333333, 0.0301178471,
0.0736098328666667, 0.0329723832, 0.0455095300666667, 0.065151615,
0.0979033533333333, 0.0247176775, 0.0335825226666667, 0.027385248,
0.0462643053333333, 0.037272505, 0.0652166726666667, 0.043005086,
0.061848328, 0.031106749, 0.0275656054, 0.026701889, 0.0373967466666667,
0.028998468, 0.03219287, 0.0597213356, 0.0851717708333333,
0.030286913, 0.0779058462666667, 0.043368508, 0.051437624,
0.029002474, 0.0479204566666667, 0.094555739, 0.0856268291666667,
0.031908514, 0.0310441326666667, 0.036311762, 0.0496942306666667,
0.054625148, 0.0482682121666667), upperCI = c(0.116082073022708,
0.139632763787946, 0.0315087794760623, 0.0727058964327625,
0.0468512606854127, 0.116787586356955, 0.0444933233012107,
0.062820743812494, 0.0858551911272202, 0.136013260005381,
0.0327074347874691, 0.0460471773903695, 0.035302995136302,
0.0740077338495226, 0.0641795522210299, 0.131047110446756,
0.0572545979325947, 0.0809511078363974, 0.0414215170576924,
0.0341480438532189, 0.0382253716300962, 0.0519626825555577,
0.0377955915789704, 0.0430125127419472, 0.0903928001427357,
0.114245467448517, 0.0461054194398361, 0.129350863514659,
0.0635159480110737, 0.0717647837071829, 0.0371919026867606,
0.0615899295823839, 0.170222051412597, 0.128502458351433,
0.046712862081242, 0.0388340720489338, 0.0574188259607336,
0.0786845830951613, 0.0844193698576058, 0.0784830058409822
), lowerCI = c(0.0356594529772922, 0.0419399734120541, 0.0129225427239377,
0.0256998206339042, 0.0133844335145873, 0.0304320793763786,
0.0214514430987893, 0.0281983163208393, 0.0444480388727798,
0.059793446661286, 0.0167279202125309, 0.0211178679429639,
0.019467500863698, 0.0185208768171441, 0.0103654577789701,
-0.000613765113422152, 0.0287555740674053, 0.0427455481636026,
0.0207919809423076, 0.0209831669467811, 0.0151784063699038,
0.0228308107777757, 0.0202013444210296, 0.0213732272580528,
0.0290498710572643, 0.0560980742181497, 0.0144684065601638,
0.0264608290186746, 0.0232210679889263, 0.0311104642928171,
0.0208130453132394, 0.0342509837509495, 0.018889426587403,
0.0427511999819006, 0.017104165918758, 0.0232541932843995,
0.0152046980392664, 0.0207038782381721, 0.0248309261423941,
0.0180534184923511), CondLevel = c("EyeClose-Haptic2", "EyeClose-Haptic3",
"mixed-Haptic_Visual2", "mixed-Haptic_Visual3", "only-Haptic2",
"only-Haptic3", "only-Visual2", "only-Visual3", "EyeClose-Haptic2",
"EyeClose-Haptic3", "mixed-Haptic_Visual2", "mixed-Haptic_Visual3",
"only-Haptic2", "only-Haptic3", "only-Visual2", "only-Visual3",
"EyeClose-Haptic2", "EyeClose-Haptic3", "mixed-Haptic_Visual2",
"mixed-Haptic_Visual3", "only-Haptic2", "only-Haptic3", "only-Visual2",
"only-Visual3", "EyeClose-Haptic2", "EyeClose-Haptic3", "mixed-Haptic_Visual2",
"mixed-Haptic_Visual3", "only-Haptic2", "only-Haptic3", "only-Visual2",
"only-Visual3", "EyeClose-Haptic2", "EyeClose-Haptic3", "mixed-Haptic_Visual2",
"mixed-Haptic_Visual3", "only-Haptic2", "only-Haptic3", "only-Visual2",
"only-Visual3")), .Names = c("Subject", "X00.conditionName",
"X03.totalTargetNumber", "Accuracy", "upperCI", "lowerCI", "CondLevel"
), row.names = c(NA, -40L), class = "data.frame")
require(ggplot2)
pdf("Pilot2.pdf", w = 12, h = 8)
limits <- aes(ymax = upperCI, ymin=lowerCI)
BaseLayer = ggplot(data = Final, aes (x = X00.conditionName, y = Accuracy, color = Subject, group = Subject ))
BaseLayer + geom_pointrange(limits, position=position_dodge(width=1), size = 1.5) +
theme(axis.text=element_text(size=14), axis.title=element_text(size=14), axis.text.x = element_text(angle = 25, hjust = 1)) +
facet_grid (.~X03.totalTargetNumber) + ggtitle ("Pilot 2") + xlab ("Condition")
dev.off()
As you can see the x-axis is discrete, and the points are very "crowded", so that it is difficult to tell apart the different categories.
Is there a way to increase the space between the different categories ?
The best solutoin is to use facets to create 8 separate tall and skinny plots with all these features that are separated by a thin white gutter between them with a solid label at the top. You could keep or lose X-axis labels. It creates one figure of 8 graphs that communicates better than on big graph.
Like this:Stack-ggplot2-geom-pointrange-facet-grid-with-coord-flip
except yours would be verticle

R GGPLOT - Add Averages of Each Series Contained in Facet Wrap

I have a dataset described as follows: I used 3 different standards, that I loaded 3 different times into the test apparatus where I took 10 measurements for each combination of standard & loading. I have been able to plot the data where I depict each loading as a different series and I do a facet wrap based on the standard. I now want to add the average of each loading for each standard to the chart and I cannot seem to do so.
My data first (LatexStandards_GammaSummary):
structure(list(Standard = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L), .Label = c("0.5 µm", "1 µm", "4 µm"), class = "factor"),
Loading = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L), .Label = c("L1", "L2", "L3"), class = "factor"),
Gamma = c(0.66716, 0.67899, 0.67286, 0.67527, 0.67327, 0.67396,
0.68518, 0.66993, 0.65695, 0.67583, 0.68428, 0.68807, 0.68862,
0.67403, 0.68282, 0.69051, 0.68571, 0.67531, 0.68146, 0.68367,
0.68348, 0.68344, 0.68768, 0.68189, 0.68253, 0.6836, 0.68388,
0.68645, 0.67551, 0.67897, 0.62186, 0.63639, 0.62981, 0.63896,
0.61639, 0.62586, 0.6226, 0.63984, 0.63112, 0.63279, 0.61764,
0.63829, 0.62712, 0.62563, 0.62233, 0.63423, 0.62621, 0.62251,
0.6287, 0.6375, 0.62774, 0.64823, 0.62692, 0.63093, 0.6223,
0.62713, 0.62279, 0.63341, 0.63451, 0.63072, 0.61586, 0.71059,
0.7198, 0.57358, 0.66188, 0.7624, 0.71269, 0.74395, 0.75922,
0.70551, 0.535, 0.59343, 0.62455, 0.72823, 0.65101, 0.56216,
0.5248, 0.54717, 0.6283, 0.63807, 0.53681, 0.54385, 0.58027,
0.69051, 0.70548, 0.61578, 0.65215, 0.68302, 0.72091, 0.58527
)), .Names = c("Standard", "Loading", "Gamma"), class = "data.frame", row.names = c(NA,
-90L))
The code I use to produce the original facet wrap ggplot:
# input data
inpdata <- LatexStandards_GammaSummary
# basic plot set up
plotout<-ggplot(data=inpdata,aes(x=Loading,y=Gamma))
# data sets
dataset1<-geom_point(aes(color=Loading),
position = "jitter")
wrapon<-facet_wrap(~Standard)
# axis labels
xlbl <- xlab("")
ylbl <- ylab("g")
# theme mods
basetheme <- theme_bw()
# x axis
theme_xaxis <- theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank()
)
number_format_xaxis <- ""
# y axis
theme_yaxis <- theme(
axis.title.y=element_text(family="GreekC",size=14)
)
number_format_yaxis <- function(x){format(x,digits=1,nsmall=1,scientific=FALSE)}
scale_yaxis <- scale_y_continuous(labels=number_format_yaxis,limits=c(0.4,0.9))
# legend
theme_legend <- theme(
legend.position = "bottom",
legend.margin = unit(-0.5,"cm"),
legend.key = element_blank(),
legend.text = element_text(size = 14),
legend.title = element_text(size = 14, face = "plain")
)
# wrapping items
theme_wrapping = theme(
strip.background = element_blank(),
strip.text = element_text(size = 14)
)
# panel items
theme_panel = theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()
)
plotout<-plotout +
dataset1 +
wrapon +
xlbl +
ylbl +
basetheme +
theme_xaxis +
theme_yaxis +
scale_yaxis +
theme_legend +
theme_wrapping +
theme_panel
plotout
Thank you for your help!
You will likely be best served by generating the summary data yourself, then plotting it. Using dplyr, here is an approach to calculate the averages:
avgLines <-
inpdata %>%
group_by(Standard, Loading) %>%
summarise(Gamma = mean(Gamma))
Which gives:
Standard Loading Gamma
1 0.5 µm L1 0.672940
2 0.5 µm L2 0.683448
3 0.5 µm L3 0.682743
4 1 µm L1 0.629562
5 1 µm L2 0.628016
6 1 µm L3 0.630468
7 4 µm L1 0.696548
8 4 µm L2 0.603272
9 4 µm L3 0.631405
Then, we can add this to the plot object you generated, setting the range of the segment you want to include (the facet_wrap from your plot object is applied here as well):
plotout +
geom_segment(
aes(y = Gamma
, yend = Gamma
, color=Loading
, x = as.numeric(Loading) - 0.5
, xend = as.numeric(Loading) + 0.5
)
, data = avgLines)
(Of note, much of the theme setting you included is not required for a minimally working plot -- you may have gotten quicker responses if you had trimmed the example down to just the portions relevant to what you wanted to generate.)

Order Stacked Bar Graph by sum / total of all subgroups

My colleague and I are trying to order a stacked bar graph based on the y-values instead of alphabetically by the x-values.
The sample data is:
library(ggplot2)
samp.data <- structure(list(fullname = c("LJ", "PR",
"JB", "AA", "NS",
"MJ", "FT", "DA", "DR",
"AB", "BA", "RJ", "BA2",
"AR", "GG", "RA", "DK",
"DA2", "BJ2", "BK", "HN",
"WA2", "AE2", "JJ2"), I = c(2L,
1L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L), S = c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 2L, 3L, 3L,
3L), D = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 2L, 3L, 3L), C = c(0L, 2L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
2L, 3L, 3L, 3L, 3L)), .Names = c("fullname", "I", "S", "D", "C"
), class = "data.frame", row.names = c(NA, 24L))
md <- reshape2::melt(samp.data, id = (c("fullname")))
ggplot(data = md, aes(x = fullname, y = value, fill = variable)) +
geom_col()
But I ultimately want to sort by the sum of the 4 variables (I, S, D, and C) instead of the alphabetical order of the fullnames.
The general (non ggplot-specific) answer is to use reorder() to reset the factor levels in a categorical column, based on some function of the other columns.
## Examine the default factor order
levels(samp.data$fullname)
## Reorder fullname based on the the sum of the other columns
samp.data$fullname <- reorder(samp.data$fullname, rowSums(samp.data[-1]))
## Examine the new factor order
levels(samp.data$fullname)
attributes(samp.data$fullname)
Then just replot, using code from the original question
md <- melt(samp.data, id=(c("fullname")))
temp.plot<-ggplot(data=md, aes(x=fullname, y=value, fill=variable) ) +
geom_bar()+
theme(axis.text.x=theme_text(angle=90)) +
labs(title = "Score Distribtion")
## ggsave(temp.plot,filename="test.png")
A much simpler solution is to change the underlying function in reorder:
ggplot(data = md, aes(x = reorder(fullname, value, sum), y = value, fill = variable)) +
geom_col()

Resources