Reordering Boxplot in ggplot2 - ggplot2

I have the following test data, and would like to plot them in descending order somehow my codes doesn't produce the desired plot
<table><tbody><tr><th>PAHs</th><th>Variable</th><th>Value</th></tr><tr><td>TB1</td><td>Nap</td><td>110</td></tr><tr><td>TB3</td><td>Nap</td><td>200</td></tr><tr><td>TB6</td><td>Nap</td><td>110</td></tr><tr><td>TB8</td><td>Nap</td><td>20</td></tr><tr><td>ND1</td><td>Nap</td><td>20</td></tr><tr><td>ND3</td><td>Nap</td><td>10</td></tr><tr><td>ND5</td><td>Nap</td><td>0</td></tr><tr><td>ND8</td><td>Nap</td><td>0</td></tr><tr><td>QT1</td><td>Nap</td><td>0</td></tr><tr><td>QT4</td><td>Nap</td><td>10</td></tr><tr><td>QT5</td><td>Nap</td><td>0</td></tr><tr><td>QT9</td><td>Nap</td><td>10</td></tr><tr><td>PT1</td><td>Nap</td><td>20</td></tr><tr><td>PT2</td><td>Nap</td><td>0</td></tr><tr><td>PT4</td><td>Nap</td><td>0</td></tr><tr><td>TB1</td><td>AcNP</td><td>20</td></tr><tr><td>TB3</td><td>AcNP</td><td>150</td></tr><tr><td>TB6</td><td>AcNP</td><td>70</td></tr><tr><td>TB8</td><td>AcNP</td><td>10</td></tr><tr><td>ND1</td><td>AcNP</td><td>0</td></tr><tr><td>ND3</td><td>AcNP</td><td>0</td></tr><tr><td>ND5</td><td>AcNP</td><td>0</td></tr><tr><td>ND8</td><td>AcNP</td><td>0</td></tr><tr><td>QT1</td><td>AcNP</td><td>0</td></tr><tr><td>QT4</td><td>AcNP</td><td>10</td></tr><tr><td>QT5</td><td>AcNP</td><td>0</td></tr><tr><td>QT9</td><td>AcNP</td><td>10</td></tr><tr><td>PT1</td><td>AcNP</td><td>30</td></tr><tr><td>PT2</td><td>AcNP</td><td>10</td></tr><tr><td>PT4</td><td>AcNP</td><td>0</td></tr><tr><td>TB1</td><td>AcN</td><td>130</td></tr><tr><td>TB3</td><td>AcN</td><td>110</td></tr><tr><td>TB6</td><td>AcN</td><td>150</td></tr><tr><td>TB8</td><td>AcN</td><td>10</td></tr><tr><td>ND1</td><td>AcN</td><td>40</td></tr><tr><td>ND3</td><td>AcN</td><td>0</td></tr><tr><td>ND5</td><td>AcN</td><td>0</td></tr><tr><td>ND8</td><td>AcN</td><td>10</td></tr><tr><td>QT1</td><td>AcN</td><td>0</td></tr><tr><td>QT4</td><td>AcN</td><td>10</td></tr><tr><td>QT5</td><td>AcN</td><td>0</td></tr><tr><td>QT9</td><td>AcN</td><td>10</td></tr><tr><td>PT1</td><td>AcN</td><td>150</td></tr><tr><td>PT2</td><td>AcN</td><td>10</td></tr><tr><td>PT4</td><td>AcN</td><td>10</td></tr><tr><td>TB1</td><td>Fl</td><td>70</td></tr><tr><td>TB3</td><td>Fl</td><td>80</td></tr><tr><td>TB6</td><td>Fl</td><td>0</td></tr><tr><td>TB8</td><td>Fl</td><td>0</td></tr><tr><td>ND1</td><td>Fl</td><td>0</td></tr><tr><td>ND3</td><td>Fl</td><td>10</td></tr><tr><td>ND5</td><td>Fl</td><td>0</td></tr><tr><td>ND8</td><td>Fl</td><td>10</td></tr><tr><td>QT1</td><td>Fl</td><td>0</td></tr><tr><td>QT4</td><td>Fl</td><td>30</td></tr><tr><td>QT5</td><td>Fl</td><td>0</td></tr><tr><td>QT9</td><td>Fl</td><td>0</td></tr><tr><td>PT1</td><td>Fl</td><td>0</td></tr><tr><td>PT2</td><td>Fl</td><td>10</td></tr><tr><td>PT4</td><td>Fl</td><td>0</td></tr><tr><td>TB1</td><td>An</td><td>100</td></tr><tr><td>TB3</td><td>An</td><td>230</td></tr><tr><td>TB6</td><td>An</td><td>110</td></tr><tr><td>TB8</td><td>An</td><td>110</td></tr><tr><td>ND1</td><td>An</td><td>130</td></tr><tr><td>ND3</td><td>An</td><td>10</td></tr><tr><td>ND5</td><td>An</td><td>10</td></tr><tr><td>ND8</td><td>An</td><td>40</td></tr><tr><td>QT1</td><td>An</td><td>10</td></tr><tr><td>QT4</td><td>An</td><td>60</td></tr><tr><td>QT5</td><td>An</td><td>0</td></tr><tr><td>QT9</td><td>An</td><td>110</td></tr><tr><td>PT1</td><td>An</td><td>150</td></tr><tr><td>PT2</td><td>An</td><td>30</td></tr><tr><td>PT4</td><td>An</td><td>30</td></tr><tr><td>TB1</td><td>PhA</td><td>100</td></tr><tr><td>TB3</td><td>PhA</td><td>240</td></tr><tr><td>TB6</td><td>PhA</td><td>200</td></tr><tr><td>TB8</td><td>PhA</td><td>110</td></tr><tr><td>ND1</td><td>PhA</td><td>130</td></tr><tr><td>ND3</td><td>PhA</td><td>40</td></tr><tr><td>ND5</td><td>PhA</td><td>10</td></tr><tr><td>ND8</td><td>PhA</td><td>40</td></tr><tr><td>QT1</td><td>PhA</td><td>0</td></tr><tr><td>QT4</td><td>PhA</td><td>70</td></tr><tr><td>QT5</td><td>PhA</td><td>0</td></tr><tr><td>QT9</td><td>PhA</td><td>130</td></tr><tr><td>PT1</td><td>PhA</td><td>150</td></tr><tr><td>PT2</td><td>PhA</td><td>30</td></tr><tr><td>PT4</td><td>PhA</td><td>40</td></tr><tr><td>TB1</td><td>FlA</td><td>20</td></tr><tr><td>TB3</td><td>FlA</td><td>20</td></tr><tr><td>TB6</td><td>FlA</td><td>60</td></tr><tr><td>TB8</td><td>FlA</td><td>30</td></tr><tr><td>ND1</td><td>FlA</td><td>30</td></tr><tr><td>ND3</td><td>FlA</td><td>0</td></tr><tr><td>ND5</td><td>FlA</td><td>0</td></tr><tr><td>ND8</td><td>FlA</td><td>0</td></tr><tr><td>QT1</td><td>FlA</td><td>0</td></tr><tr><td>QT4</td><td>FlA</td><td>20</td></tr><tr><td>QT5</td><td>FlA</td><td>0</td></tr><tr><td>QT9</td><td>FlA</td><td>30</td></tr><tr><td>PT1</td><td>FlA</td><td>30</td></tr><tr><td>PT2</td><td>FlA</td><td>0</td></tr><tr><td>PT4</td><td>FlA</td><td>20</td></tr><tr><td>TB1</td><td>Py</td><td>20</td></tr><tr><td>TB3</td><td>Py</td><td>20</td></tr><tr><td>TB6</td><td>Py</td><td>60</td></tr><tr><td>TB8</td><td>Py</td><td>30</td></tr><tr><td>ND1</td><td>Py</td><td>30</td></tr><tr><td>ND3</td><td>Py</td><td>0</td></tr><tr><td>ND5</td><td>Py</td><td>0</td></tr><tr><td>ND8</td><td>Py</td><td>0</td></tr><tr><td>QT1</td><td>Py</td><td>0</td></tr><tr><td>QT4</td><td>Py</td><td>20</td></tr><tr><td>QT5</td><td>Py</td><td>0</td></tr><tr><td>QT9</td><td>Py</td><td>30</td></tr><tr><td>PT1</td><td>Py</td><td>30</td></tr><tr><td>PT2</td><td>Py</td><td>0</td></tr><tr><td>PT4</td><td>Py</td><td>20</td></tr><tr><td>TB1</td><td>BaA</td><td>0</td></tr><tr><td>TB3</td><td>BaA</td><td>10</td></tr><tr><td>TB6</td><td>BaA</td><td>0</td></tr><tr><td>TB8</td><td>BaA</td><td>10</td></tr><tr><td>ND1</td><td>BaA</td><td>30</td></tr><tr><td>ND3</td><td>BaA</td><td>0</td></tr><tr><td>ND5</td><td>BaA</td><td>0</td></tr><tr><td>ND8</td><td>BaA</td><td>0</td></tr><tr><td>QT1</td><td>BaA</td><td>0</td></tr><tr><td>QT4</td><td>BaA</td><td>0</td></tr><tr><td>QT5</td><td>BaA</td><td>0</td></tr><tr><td>QT9</td><td>BaA</td><td>10</td></tr><tr><td>PT1</td><td>BaA</td><td>0</td></tr><tr><td>PT2</td><td>BaA</td><td>0</td></tr><tr><td>PT4</td><td>BaA</td><td>0</td></tr><tr><td>TB1</td><td>Chy</td><td>0</td></tr><tr><td>TB3</td><td>Chy</td><td>0</td></tr><tr><td>TB6</td><td>Chy</td><td>0</td></tr><tr><td>TB8</td><td>Chy</td><td>0</td></tr><tr><td>ND1</td><td>Chy</td><td>30</td></tr><tr><td>ND3</td><td>Chy</td><td>0</td></tr><tr><td>ND5</td><td>Chy</td><td>0</td></tr><tr><td>ND8</td><td>Chy</td><td>0</td></tr><tr><td>QT1</td><td>Chy</td><td>0</td></tr><tr><td>QT4</td><td>Chy</td><td>0</td></tr><tr><td>QT5</td><td>Chy</td><td>0</td></tr><tr><td>QT9</td><td>Chy</td><td>0</td></tr><tr><td>PT1</td><td>Chy</td><td>10</td></tr><tr><td>PT2</td><td>Chy</td><td>0</td></tr><tr><td>PT4</td><td>Chy</td><td>0</td></tr><tr><td>TB1</td><td>BpFlA</td><td>20</td></tr><tr><td>TB3</td><td>BpFlA</td><td>10</td></tr><tr><td>TB6</td><td>BpFlA</td><td>30</td></tr><tr><td>TB8</td><td>BpFlA</td><td>30</td></tr><tr><td>ND1</td><td>BpFlA</td><td>40</td></tr><tr><td>ND3</td><td>BpFlA</td><td>0</td></tr><tr><td>ND5</td><td>BpFlA</td><td>0</td></tr><tr><td>ND8</td><td>BpFlA</td><td>0</td></tr><tr><td>QT1</td><td>BpFlA</td><td>0</td></tr><tr><td>QT4</td><td>BpFlA</td><td>0</td></tr><tr><td>QT5</td><td>BpFlA</td><td>0</td></tr><tr><td>QT9</td><td>BpFlA</td><td>40</td></tr><tr><td>PT1</td><td>BpFlA</td><td>40</td></tr><tr><td>PT2</td><td>BpFlA</td><td>0</td></tr><tr><td>PT4</td><td>BpFlA</td><td>10</td></tr><tr><td>TB1</td><td>BkFlA</td><td>20</td></tr><tr><td>TB3</td><td>BkFlA</td><td>10</td></tr><tr><td>TB6</td><td>BkFlA</td><td>20</td></tr><tr><td>TB8</td><td>BkFlA</td><td>30</td></tr><tr><td>ND1</td><td>BkFlA</td><td>30</td></tr><tr><td>ND3</td><td>BkFlA</td><td>10</td></tr><tr><td>ND5</td><td>BkFlA</td><td>0</td></tr><tr><td>ND8</td><td>BkFlA</td><td>0</td></tr><tr><td>QT1</td><td>BkFlA</td><td>0</td></tr><tr><td>QT4</td><td>BkFlA</td><td>10</td></tr><tr><td>QT5</td><td>BkFlA</td><td>0</td></tr><tr><td>QT9</td><td>BkFlA</td><td>20</td></tr><tr><td>PT1</td><td>BkFlA</td><td>30</td></tr><tr><td>PT2</td><td>BkFlA</td><td>10</td></tr><tr><td>PT4</td><td>BkFlA</td><td>10</td></tr><tr><td>TB1</td><td>BaP</td><td>10</td></tr><tr><td>TB3</td><td>BaP</td><td>0</td></tr><tr><td>TB6</td><td>BaP</td><td>0</td></tr><tr><td>TB8</td><td>BaP</td><td>0</td></tr><tr><td>ND1</td><td>BaP</td><td>20</td></tr><tr><td>ND3</td><td>BaP</td><td>0</td></tr><tr><td>ND5</td><td>BaP</td><td>0</td></tr><tr><td>ND8</td><td>BaP</td><td>10</td></tr><tr><td>QT1</td><td>BaP</td><td>0</td></tr><tr><td>QT4</td><td>BaP</td><td>0</td></tr><tr><td>QT5</td><td>BaP</td><td>0</td></tr><tr><td>QT9</td><td>BaP</td><td>0</td></tr><tr><td>PT1</td><td>BaP</td><td>0</td></tr><tr><td>PT2</td><td>BaP</td><td>0</td></tr><tr><td>PT4</td><td>BaP</td><td>0</td></tr><tr><td>TB1</td><td>dBahA</td><td>40</td></tr><tr><td>TB3</td><td>dBahA</td><td>10</td></tr><tr><td>TB6</td><td>dBahA</td><td>20</td></tr><tr><td>TB8</td><td>dBahA</td><td>20</td></tr><tr><td>ND1</td><td>dBahA</td><td>20</td></tr><tr><td>ND3</td><td>dBahA</td><td>0</td></tr><tr><td>ND5</td><td>dBahA</td><td>0</td></tr><tr><td>ND8</td><td>dBahA</td><td>0</td></tr><tr><td>QT1</td><td>dBahA</td><td>0</td></tr><tr><td>QT4</td><td>dBahA</td><td>20</td></tr><tr><td>QT5</td><td>dBahA</td><td>0</td></tr><tr><td>QT9</td><td>dBahA</td><td>0</td></tr><tr><td>PT1</td><td>dBahA</td><td>0</td></tr><tr><td>PT2</td><td>dBahA</td><td>0</td></tr><tr><td>PT4</td><td>dBahA</td><td>10</td></tr><tr><td>TB1</td><td>IP</td><td>0</td></tr><tr><td>TB3</td><td>IP</td><td>0</td></tr><tr><td>TB6</td><td>IP</td><td>0</td></tr><tr><td>TB8</td><td>IP</td><td>0</td></tr><tr><td>ND1</td><td>IP</td><td>0</td></tr><tr><td>ND3</td><td>IP</td><td>0</td></tr><tr><td>ND5</td><td>IP</td><td>0</td></tr><tr><td>ND8</td><td>IP</td><td>0</td></tr><tr><td>QT1</td><td>IP</td><td>0</td></tr><tr><td>QT4</td><td>IP</td><td>0</td></tr><tr><td>QT5</td><td>IP</td><td>0</td></tr><tr><td>QT9</td><td>IP</td><td>0</td></tr><tr><td>PT1</td><td>IP</td><td>0</td></tr><tr><td>PT2</td><td>IP</td><td>0</td></tr><tr><td>PT4</td><td>IP</td><td>0</td></tr><tr><td>TB1</td><td>BghiP</td><td>0</td></tr><tr><td>TB3</td><td>BghiP</td><td>0</td></tr><tr><td>TB6</td><td>BghiP</td><td>0</td></tr><tr><td>TB8</td><td>BghiP</td><td>0</td></tr><tr><td>ND1</td><td>BghiP</td><td>10</td></tr><tr><td>ND3</td><td>BghiP</td><td>0</td></tr><tr><td>ND5</td><td>BghiP</td><td>0</td></tr><tr><td>ND8</td><td>BghiP</td><td>0</td></tr><tr><td>QT1</td><td>BghiP</td><td>0</td></tr><tr><td>QT4</td><td>BghiP</td><td>0</td></tr><tr><td>QT5</td><td>BghiP</td><td>0</td></tr><tr><td>QT9</td><td>BghiP</td><td>0</td></tr><tr><td>PT1</td><td>BghiP</td><td>0</td></tr><tr><td>PT2</td><td>BghiP</td><td>0</td></tr><tr><td>PT4</td><td>BghiP</td><td>0</td></tr></tbody></table>
library(tidyverse)
library(cowplot)
library(scales)
scale_y_log2 <- function (...)
{scale_y_continuous(..., trans = log2_trans())
}
alexa<-read.csv(file.choose(),header=T, sep=",")
require(reshape2)
alexa.m<-melt(alexa,id_var="PAHs")
alexa.m$PAHs<-factor(alexa.m$PAHs,rev(unique(alexa.m$PAHs)),ordered=TRUE)
ggplot(alexa.m, aes(x=reorder(variable,-value),y=value))+scale_y_log2()+
stat_boxplot(geom ='errorbar') +
geom_boxplot()+xlab(" ")+ ylab("PAHs concentration(µg/L)") +
theme(axis.text.y = element_text(size=11),
axis.text.x = element_text(size=11),
axis.title.y = element_text(size=11),panel.background = element_blank(), panel.border = element_rect(colour = "black", fill=NA, size=1))

Your plot is already in descending order - in means.
If you check ?reorder, the default is to order by mean.
You can specify how you want function you want to use to reorder the values of interest by FUN.
For example:
The line below reorders by median of value in an ascending manner.
aes(reorder(variable, value, FUN = median), value)
The line below reorders by max value in an ascending manner.
aes(reorder(variable, value, FUN = max), value)
For descending order, add a minus sign like you already did
aes(reorder(variable, -value, FUN = median), value)
aes(reorder(variable, -value, FUN = min), value)

Related

Adding stat = count on top of histogram in ggplot

I've seen some other examples (especially using geom_col() and stat_bin()) to add frequency or count numbers on top of bars. I'm trying to get this to work with geom_histogram() where I have a discrete (string), not continuous, x variable.
library(tidyverse)
d <- cars |>
mutate( discrete_var = factor(speed))
ggplot(d, aes(x = discrete_var)) +
geom_histogram(stat = "count") +
stat_bin(binwidth=1, geom='text', color='white', aes(label=..count..),
position=position_stack(vjust = 0.5)) +
Gives me an error because StatBin requires a continuous x variable. Any quick fix ideas?
The error message gives you the answer: ! StatBin requires a continuous x variable: the x variable is discrete.Perhaps you want stat="count"?
So instead of stat_bin() use stat_count()
And for further reference here is a reproducible example:
library(tidyverse)
d <- cars |>
mutate( discrete_var = factor(speed))
ggplot(data = d,
aes(x = discrete_var)) +
geom_histogram(stat = "count") +
stat_count(binwidth = 1,
geom = 'text',
color = 'white',
aes(label = ..count..),
position = position_stack(vjust = 0.5))

ggplot facet different Y axis order based on value

I have a faceted plot wherein I'd like to have the Y-axis labels and the associated values appear in descending order of values (and thereby changing the order of the labels) for each facet. What I have is this, but the order of the labels (and the corresponding values) is the same for each facet.
ggplot(rf,
aes(x = revenues,
y = reorder(AgencyName, revenues))) +
geom_point(stat = "identity",
aes(color = AgencyName),
show.legend = FALSE) +
xlab(NULL) +
ylab(NULL) +
scale_x_continuous(label = scales::comma) +
facet_wrap(~year, ncol = 3, scales = "free_y") +
theme_minimal()
Can someone point me to the solution?
The functions reorder_within and scale_*_reordered from the tidytext package might come in handy.
reorder_within recodes the values into a factor with strings in the form of "VARIABLE___WITHIN". This factor is ordered by the values in each group of WITHIN.
scale_*_reordered removes the "___WITHIN" suffix when plotting the axis labels.
Add scales = "free_y" in facet_wrap to make it work as expected.
Here is an example with generated data:
library(tidyverse)
# Generate data
df <- expand.grid(
year = 2019:2021,
group = paste("Group", toupper(letters[1:8]))
)
set.seed(123)
df$value <- rnorm(nrow(df), mean = 10, sd = 2)
df %>%
mutate(group = tidytext::reorder_within(group, value, within = year)) %>%
ggplot(aes(value, group)) +
geom_point() +
tidytext::scale_y_reordered() +
facet_wrap(vars(year), scales = "free_y")

specify drawing order with geom_line

I'm trying avoid overlapping lines in a color scheme. Excuse the ugly plot, but in the following, how do I have the lines drawn in the order of y1, y2, y1Smooth, and y2Smooth?
foo <- tibble(x=1:100,y1=rnorm(100,1),y2=rnorm(100,2)) %>%
mutate(y1Spline = smooth.spline(y1,spar=0.5)$y,
y2Spline = smooth.spline(y2,spar=0.5)$y) %>%
pivot_longer(cols=-x)
cols = c("darkgreen","darkblue")
ggplot(foo,aes(x=x,y=value,col=name, alpha=name)) +
geom_line() +
scale_color_manual(values=rep(cols,each=2)) +
scale_alpha_manual(values=c(0.5,1,0.5,1))
Practically-speaking, the order of legend items for a character variable will be equal to the order of the levels when that column is coerced into a factor. So, the default ordering is exemplified by this:
> levels(factor(foo$name))
[1] "y1" "y1Spline" "y2" "y2Spline"
Look familiar? The answer is to set the levels yourself before plotting:
library(ggplot2)
set.seed(8675309)
foo <- tibble(x=1:100,y1=rnorm(100,1),y2=rnorm(100,2)) %>%
mutate(y1Spline = smooth.spline(y1,spar=0.5)$y,
y2Spline = smooth.spline(y2,spar=0.5)$y) %>%
pivot_longer(cols=-x)
# force factor level order
foo$name <- factor(foo$name, levels=c("y1", "y2", "y1Spline", "y2Spline"))
cols = c("darkgreen","darkblue")
ggplot(foo,aes(x=x,y=value,col=name, alpha=name)) +
geom_line() +
scale_color_manual(values=rep(cols,each=2)) +
scale_alpha_manual(values=c(0.5,1,0.5,1))
A special note, the lines are drawn in that order - this means the line for "y1" will be on top of "y1Spline". If you want the lines drawn in that order, but the legend reversed, you'll need to add something like guides(color=guide_legend(reverse=TRUE)) at the end of your plot code.

ggplot - line ordering, one line on top of the other

Here is my example:
library(ggplot2)
forecast <- c(2,2,1,2,2,3,2,3,3,3,3)
actual <- c(2,2,1,2,2,3,2,3,2,2,1)
my_df <- data.frame(forecast = forecast, actual = actual)
my_df$seq_order <- as.factor(1:NROW(my_df))
my_df <-gather(my_df, "line_type", "value", -seq_order)
ggplot(data=my_df, aes(x=seq_order, y = value,
colour = line_type, group=line_type))+geom_line()+theme(legend.position="bottom")
Here is how it looks:
I would like to have red line to be on top of blue line everywhere where they coincide. I tried scale_color_manual(values = c("forecast" = "red" ,"actual" = "blue")), but it did not work.
Change the factor level order. Don't forget to change the group too.
See this related thread, why I used scales::hue() etc
library(tidyverse)
forecast <- c(2,2,1,2,2,3,2,3,3,3,3)
actual <- c(2,2,1,2,2,3,2,3,2,2,1)
my_df <- data.frame(forecast = forecast, actual = actual, seq_order = 1:11)
my_df <-gather(my_df, line_type, value, -seq_order) %>% mutate(type = factor(line_type, levels = c('forecast','actual')))
ggplot(data=my_df, aes(x=seq_order, y = value,
colour = type, group = type)) +
geom_line()+
theme(legend.position="bottom") +
scale_color_manual(values = rev(scales::hue_pal()(2)))
Created on 2020-03-24 by the reprex package (v0.3.0)

Connect observations (dots and lines) without using ggpaired

I created a bar chart using geom_bar with "Group" on the x-axis (Female, Male), and "Values" on the y-axis. Group is further subdivided into "Session" such that there is "Session 1" and "Session 2" for both Male and Female (i.e. four bars in total).
Since all participants participated in Session 1 and 2, I overlayed a dotplot (geom_dot) over each of the four bars, to represent the individual data.
I am now trying to connect the observations for all participants ("PID"), between session 1 and 2. In other words, there should be lines connecting several sets of two-points on the "Male" portion of the x-axis (i.e. per participant), and "Female portion".
I tried this with "geom_line" (below) but to no avail (instead, it created a single vertical line in the middle of "Male" and another in the middle of "Female"). I'm not too sure how to fix this.
See code below:
ggplot(data_foo, aes(x=factor(Group),y=Values, colour = factor(Session), fill = factor(Session))) +
geom_bar(stat = "summary", fun.y = "mean", position = "dodge") +
geom_dotplot(binaxis = "y", stackdir = "center", dotsize = 1.0, position = "dodge", fill = "black") +
geom_line(aes(group = PID), colour="dark grey") +
labs(title='My Data',x='Group',y='Values') +
theme_light()
Sample data (.txt)
data_foo <- readr::read_csv("PID,Group,Session,Values
P1,F,1,14
P2,F,1,13
P3,F,1,16
P4,M,1,18
P5,F,1,20
P6,M,1,27
P7,M,1,19
P8,M,1,11
P9,F,1,28
P10,F,1,20
P11,F,1,24
P12,M,1,10
P1,F,2,26
P2,F,2,21
P3,F,2,19
P4,M,2,13
P5,F,2,26
P6,M,2,15
P7,M,2,23
P8,M,2,23
P9,F,2,30
P10,F,2,21
P11,F,2,11
P12,M,2,19")
The trouble you have is that you want to dodge by several groups. Your geom_line does not know how to split the Group variable by session. Here are two ways to address this problem. Method 1 is probably the most "ggploty way", and a neat way of adding another grouping without making the visualisation too overcrowded. for method 2 you need to change your x variable
1) Use facet
2) Use interaction to split session for each Group. Define levels for the right bar order
I have also used geom_point instead, because geom_dot is more a specific type of histogram.
I would generally recommend to use boxplots for such plots of values like that, because bars are more appropriate for specific measures such as counts.
Method 1: Facets
library(ggplot2)
ggplot(data_foo, aes(x = Session, y = Values, fill = as.character(Session))) +
geom_bar(stat = "summary", fun.y = "mean", position = "dodge") +
geom_line(aes(group = PID)) +
geom_point(aes(group = PID), shape = 21, color = 'black') +
facet_wrap(~Group)
Created on 2020-01-20 by the reprex package (v0.3.0)
Method 2: create an interaction term in your x variable. note that you need to order the factor levels manually.
data_foo <- data_foo %>% mutate(new_x = factor(interaction(Group,Session), levels = c('F.1','F.2','M.1','M.2')))
ggplot(data_foo, aes(x = new_x, y = Values, fill = as.character(Session))) +
geom_bar(stat = "summary", fun.y = "mean", position = "dodge") +
geom_line(aes(group = PID)) +
geom_point(aes(group = PID), shape = 21, color = 'black')
Created on 2020-01-20 by the reprex package (v0.3.0)
But everything gets visually not very compelling.
I suggest doing a few visualization tips to have a more informative chart. For example, I feel like having a differentiation of colors for PID will help us track the changes of each participant for different levels of other variables. Something like:
library(ggplot2)
ggplot(data_foo, aes(x = factor(Session), y = Values, fill = factor(Session))) +
geom_bar(stat = "summary", fun.y = "mean", position = "dodge") +
geom_line(aes(group = factor(PID), colour=factor(PID)), size=2, alpha=0.7) +
geom_point(aes(group = factor(PID), colour=factor(PID)), shape = 21, size=2,show.legend = F) +
theme_bw() +
labs(x='Session',fill='Session',colour='PID')+
theme(legend.position="right") +
facet_wrap(~Group)+
scale_colour_discrete(breaks=paste0('P',1:12))
And we have the following plot:
Hope it helps.