Last Updated
Viewed 06 Times

Display single/individual observation in a sample population using R - boxplot ggplot2

I am trying to display a single observation in a population of 400 using boxplot ggplot2. I was able to display this using text, but I would like to do that using a point with a colour different from the rest. This is the code that I managed to code so far. [![I also added a picture of how the results look][1]][1]

``````
HEXACO<-data10[, c("Record","Emotionality","Extraversion", "Agreeableness",
"Conscientiousness","Openness", "Honesty_Humility")]

data_long <- gather(HEXACO, hexaco, score, Openness, Conscientiousness, Agreeableness, Extraversion,
Emotionality, Honesty_Humility, factor_key=TRUE)

Oliver <-data_long[c(1, 11, 21, 31, 41, 51), c("hexaco", "score")]

HEXACO <- ggplot(data = data_long, aes(x= hexaco, y= score)) +
geom_boxplot(width = 0.6, fill = "lightgrey", alpha = 0.5) +
coord_flip() +
theme_USGS_box()+
scale_y_continuous(breaks=c(0,0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5))+
expand_limits(y = 1:5)+
geom_text(data = Oliver, label = "Oliver",size = 3)+
labs(title = "HEXACO Profile", x = "Honesty-Humilty", y = "Score")

[![enter image description here][1]][1]

[1]: https://i.stack.imgur.com/9QYmp.png
``````

Similar Question 1 : Using ggplot2 (R) to color individual datapoints in boxplot by repeat

I am trying to show data from an experiment that has been repeated x-number of times using a boxplot. I would like to show each individual data point. In addition, to help people understand the effect of individual repeats, I would like to color the data points according to the repeat.
The following code works, but it does not allow me to color the data points.
As always any help is appreciated

sample data and code below

``````  #   conditions    repeats fold
#  A  1   7.11415E-05
#  B  1   0.094383838
#  C  1   2.13914E-05
#  D  1   1
#  E  1   0.418905744
#  F  1   0.62318476
#  A  2   0.00069782
#  B  2   0.006064881
#  C  2   0.00090097
#  D  2   1
#  E  2   1.198034711
#  F  2   1.20836637

library(ggplot2)

# Load data file from current working directory:
data
data\$conditions <- factor(data\$conditions,levels=c("A","B","C","D","E","F"))

cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

ggplot(data, aes(conditions, fold, hue=repeats)) +
geom_boxplot(outlier.size=0) +
geom_jitter( aes(conditions, fold),
position=position_jitter(width=0,height=0),
alpha=1,
size=3,
show_guide=FALSE) +
ylab("Y-AXIS") +
theme(axis.title.x=element_blank(),
axis.title.y = element_text(face='bold',size=16,vjust=1),
axis.text.x = element_text(face='bold',size=12,color='black'),
axis.text.y = element_text(face='bold',size=14,color='black'),
legend.position="none")
``````

edit:

``````> dput(data)
structure(list(conditions = structure(c(1L, 2L, 3L, 4L, 5L, 6L,
1L, 2L, 3L, 4L, 5L, 6L), .Label = c("A", "B", "C", "D", "E",
"F"), class = "factor"), repeats = c(1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L), fold = c(7.11415e-05, 0.094383838, 2.13914e-05,
1, 0.418905744, 0.62318476, 0.00069782, 0.006064881, 0.00090097,
1, 1.198034711, 1.20836637)), .Names = c("conditions", "repeats",
"fold"), row.names = c(NA, -12L), class = "data.frame")
``````

edit 2:

my code gives:

what I would like is (and if possible, allow me to control the colors):

Similar Question 2 : Boxplot in R using ggplot2

I'm new to R and have been trying to make a boxplot. A part of the data I'm using is shown

``````            h1          h2          h3          h4          h5          h6          h7          h8          h9         h10
1  0.003719430 0.002975544 0.003049933 0.003421876 0.003421876 0.003347487 0.003645042 0.003496264 0.007364472 0.009075410
2  0.003400540 0.002749373 0.003038781 0.003328188 0.003328188 0.003400540 0.003472892 0.003400540 0.007741656 0.009333398
3  0.003741387 0.002918282 0.003142765 0.003367248 0.003367248 0.003367248 0.003666559 0.003516904 0.008081396 0.008156223
4  0.003870634 0.002884002 0.003187581 0.003339370 0.003567055 0.003415265 0.003794739 0.003491160 0.008348426 0.007741268
5  0.003782963 0.002950711 0.003177689 0.003480326 0.003404667 0.003404667 0.003707304 0.003631645 0.008927793 0.007414608
6  0.003643736 0.002884624 0.003264180 0.003416002 0.003491913 0.003416002 0.003871469 0.003795558 0.009033428 0.007135649
7  0.003718600 0.003035592 0.003111482 0.003339151 0.003566821 0.003566821 0.003642710 0.003870380 0.008120209 0.008044319
8  0.003819313 0.002979064 0.003284609 0.003360995 0.003590154 0.003437382 0.003895699 0.003590154 0.008326102 0.007791398
9  0.003899334 0.002981844 0.003211216 0.003364131 0.003669961 0.003440589 0.003746419 0.003669961 0.008410328 0.007569295
10 0.003828488 0.002986220 0.003292499 0.003445639 0.003522209 0.003522209 0.003598778 0.003598778 0.008422673 0.007810115
``````

When I use the default `boxplot` command then this is what I get

``````boxplot(df)
``````

I have been trying to generate the boxplot for same data using `ggplot2` but it gives an error which I am unable to resolve. Here's what I tried.

``````library(ggplot2)
ggplot(df) + geom_boxplot()
``````

Here's the error

``````Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous
Error: Aesthetics must either be length one, or the same length as the dataProblems:df[, 6:15]
``````

I saw the ggplot2 docs for `geom_boxplot` and realize (from the example) that I need to rearrange my data like

``````col1        col2
h1   0.003719430
h1   0.003400540
h1   0.003741387
h1   0.003870634
h1   0.003782963
h1   0.003643736
h2   0.002975544
h2   0.002749373
h2   0.002918282
h2   0.002884002
h2   0.002950711
h2   0.002884624
...
``````

and use something like

``````ggplot(df, aes(factor(col1), col2)) + geom_boxplot()
``````

But that is a lot of work. I believe that there must be some way to do this automatically which I'm not able to find. Any help is appreciated.

Similar Question 3 : grouped boxplot r ggplot2

I have 5 columns of numerical data (Equipment, Hyiene.items etc) and 1 column of categorical data (A or D). I'd like to make a grouped boxplot of the numerical data grouped by category but I cannot find a way:

`````` head(sc)
Equipment Hygiene.items Patient Near.bed Far.bed Care
1         0             0       1        5       1    D
2         1             4       1        2       0    D
3         3             1       1        2       0    D
4         0             2       2        3       1    A
5         1             2       1        5       2    A
6         1             2       1        1       1    A
``````

`boxplot(sc~sc\$Care)` would seem like the most appropriate way right? I like ggplot2 but it doesn't look like i can simply do this:

``````ggplot(sc, aes(y=sc)) +
geom_boxplot(aes(fill=Care))
``````

EDIT: What I like the look of:

I think what I'm after is something like this one I made in Matlab (a long time ago):

Or the 4th graph on here: Plotly

What I have so far:

``````library(ggplot2)
library(RColorBrewer)

ggplot(melt_A,aes(x=Care,y=value,fill=Care))+geom_boxplot(ylim=c(1,6,1))+facet_grid(~variable)+
labs(x = "Care", y = "Surface contacts",color="Care" )+
scale_y_continuous(limits = c(-0, 6))+
scale_fill_brewer(palette="Purples")+
theme_bw()+
theme(strip.background=element_rect(fill="black"))+
theme(strip.text=element_text(color="white", face="bold"))
``````

Question

How can I change the Care labels from D, H, Me, to something else? e.g. Direct Care, Housekeeping, Medication round, etc...

Fixed:

I added the following to my ggplot command

``````scale_fill_brewer(palette="Purples",
labels = c("Direct care", "Housekeeping","Medication    round","Mealtimes","Miscellaneous care","Personal care"))
``````

cc