ggplot - Remove alpha legend - r

I have created a plot with the following dataset:
Locus;Island;AR;Type;Shapetype
MS1;ST;4,6315;MS;NA
MS1;FG;3,9689;MS;NA
MS1;SN;3;MS;NA
MS2;ST;2;MS;NA
MS2;FG;2;MS;NA
MS2;SN;2;MS;NA
MS3;ST;7,5199;MS;NA
MS3;FG;5,5868;MS;NA
MS3;SN;3;MS;NA
MS4;ST;2,9947;MS;NA
MS4;FG;3;MS;NA
MS4;SN;2;MS;NA
MS5;ST;9,0726;MS;NA
MS5;FG;5,6759;MS;NA
MS5;SN;2,963;MS;NA
MS6;ST;6,5779;MS;NA
MS6;FG;5,6842;MS;NA
MS6;SN;2;MS;NA
MS7;ST;2;MS;NA
MS7;FG;1;MS;NA
MS7;SN;1;MS;NA
MS8;ST;3,97;MS;NA
MS8;FG;2,9032;MS;NA
MS8;SN;1;MS;NA
MS9;ST;2;MS;NA
MS9;FG;1,9977;MS;NA
MS9;SN;2;MS;NA
MS10;ST;3,9733;MS;NA
MS10;FG;3,9971;MS;NA
MS10;SN;2;MS;NA
MS11;ST;7,4172;MS;NA
MS11;FG;5,6471;MS;NA
MS11;SN;3;MS;NA
MS12;ST;2;MS;NA
MS12;FG;2;MS;NA
MS12;SN;2;MS;NA
MS13;ST;5,6135;MS;NA
MS13;FG;3;MS;NA
MS13;SN;2;MS;NA
MT;ST;12;MT;NA
MT;FG;3;MT;NA
MT;SN;2;MT;NA
TLR1LA;ST;3,68;TLR;TLR1LA
TLR1LA;FG;4,4;TLR;TLR1LA
TLR1LA;SN;1;TLR;TLR1LA
TLR1LB;ST;3,99;TLR;TLR1LB
TLR1LB;FG;5;TLR;TLR1LB
TLR1LB;SN;1;TLR;TLR1LB
TLR2A;ST;4,9;TLR;TLR2A
TLR2A;FG;5;TLR;TLR2A
TLR2A;SN;2;TLR;TLR2A
TLR2B;ST;5,64;TLR;TLR2B
TLR2B;FG;4;TLR;TLR2B
TLR2B;SN;3;TLR;TLR2B
TLR3;ST;1;TLR;TLR3
TLR3;FG;3;TLR;TLR3
TLR3;SN;3;TLR;TLR3
TLR4;ST;1;TLR;TLR4
TLR4;FG;2,89;TLR;TLR4
TLR4;SN;2;TLR;TLR4
TLR5;ST;2,9;TLR;TLR5
TLR5;FG;2;TLR;TLR5
TLR5;SN;2;TLR;TLR5
TLR21;ST;2,91;TLR;TLR21
TLR21;FG;1;TLR;TLR21
TLR21;SN;1;TLR;TLR21
Here's the code for the plot:
ggplot(comb, aes(Island, AR, group = Locus, colour = (factor(Type)))) +
geom_line(aes(colour = factor(Type), alpha = factor(Type), size = factor(Type))) +
scale_alpha_manual(values = c("MS"=0.2, "MT"=0.2, "TLR" = 1)) +
scale_size_manual(values = c("MS"=0.5, "MT"=0.5, "TLR" = 0.3)) +
xlab("Island") +
ylab("Allelic Richness") +
scale_x_discrete(labels = c("Santiago", "Fogo", "Sao Nicolau"),
limits = c("ST", "FG", "SN")) +
geom_point(aes(shape = (factor(Shapetype)))) +
scale_shape_manual(values = c(1,2,3,4,5,6,7,8,9,10),
breaks=c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3",
"TLR4", "TLR5","TLR21", "MS", "MT")) +
scale_colour_manual(values = c("Red","Blue","Black"),
breaks=c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3",
"TLR4","TLR5","TLR21", "MS", "MT")) +
theme_bw() +
labs(shape="Functional", colour="Neutral")
The plot is okay, however, I need to remove the legend that is created for the alpha values. I have tried to use both + scale_alpha(guide = 'none')and guide = 'none', but none of them seem to work (I may be placing them in the wrong places, though). I suspect that they do not work, because of the manual adjustment of the alpha values.

Please be aware that this is not a minimal example.
Please note that your alpha legend is also your size legend, but this is very hard to see since your sizes are very similar. Set guide = 'none' in both scale_alpha_manual and scale_size_manual to remove that portion of the legend.
If you only do it in scale_alpha_manual you can actually see that the alpha becomes 1 for those lines, so it works as intended. So #Thierry's answer is correct.
Full code
ggplot(comb, aes(Island, AR, group = Locus, colour = (factor(Type)))) +
geom_line(aes(colour = factor(Type), alpha = factor(Type), size = factor(Type))) +
scale_alpha_manual(values = c("MS"=0.2, "MT"=0.2, "TLR" = 1), guide = 'none') +
scale_size_manual(values = c("MS"=0.5, "MT"=0.5, "TLR" = 0.3), guide = 'none') +
xlab("Island") +
ylab("Allelic Richness") +
scale_x_discrete(labels = c("Santiago", "Fogo", "Sao Nicolau"),
limits = c("ST", "FG", "SN")) +
geom_point(aes(shape = (factor(Shapetype)))) +
scale_shape_manual(values = c(1,2,3,4,5,6,7,8,9,10),
breaks=c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3",
"TLR4", "TLR5","TLR21", "MS", "MT")) +
scale_colour_manual(values = c("Red","Blue","Black"),
breaks=c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3",
"TLR4","TLR5","TLR21", "MS", "MT")) +
theme_bw() +
labs(shape="Functional", colour="Neutral")
Result
(Note that my y-axis is wrong because your data includes comma's and I was lazy.)

guide = "none" should do the trick
ggplot(
comb,
aes(Island, AR, group = Locus, colour = (factor(Type)))
) +
geom_line(aes(alpha = factor(Type), size = factor(Type))) +
geom_point(aes(shape = factor(Shapetype))) +
scale_x_discrete(
"Island",
labels = c("Santiago", "Fogo", "Sao Nicolau"),
limits = c("ST", "FG", "SN")
) +
ylab("Allelic Richness") +
scale_alpha_manual(values = c("MS"=0.2, "MT"=0.2, "TLR" = 1), guide = "none") +
scale_size_manual(values = c("MS"=0.5, "MT"=0.5, "TLR" = 0.3)) +
scale_shape_manual(
"Functional",
values = c(1,2,3,4,5,6,7,8,9,10),
breaks = c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3","TLR4","TLR5","TLR21", "MS", "MT")
) +
scale_colour_manual(
"Neutral",
values = c("Red","Blue","Black"),
breaks = c("TLR1LA","TLR1LB","TLR2A","TLR2B","TLR3","TLR4","TLR5","TLR21", "MS", "MT")
) +
theme_bw()

Starting with this basic plot
bp <- df %>%
ggplot(aes(column_of_interest, alpha = 0.25)) + geom_density()
from r cookbook, where bp is your ggplot -
Remove legend for a particular aesthetic (alpha):
bp + guides(alpha="none")

Related

How can I label the groups of bars on the x-axis?

How can I add labels to the groups of bars on the x- axis (which is the left side of the graph)? It is easy to label the entire axis, or to allow the labels to be generated based on the data, but I am unable to figure out how to label each group of bars, if that makes sense.
I know I could recode the item data into complete sentences, but that seems inelegant relative to making some change to the ggplot code.
I have tried using the code from a similar question on this site (Customize axis labels) scale_x_discrete(breaks = 1:5, labels=c("foo","bar","baz","phi","fum")) + but it simply causes all of the labels to disappear from my graph, and I'm not sure why. That result is worse than using scale_x_discrete(waiver()) +
First, load libraries + color palette:
library(ggplot2)
library(tidyverse)
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
And given the following data:
item <- c("none","none","none",
"low", "low", "low",
"moderatelow","moderatelow","moderatelow",
"moderate","moderate","moderate",
"greatest","greatest","greatest")
mean <- c(2.566, 2.873, 3.286, # none - Scenario A
3.911, 4.123, 4.519, # low - Scenario B
4.113, 4.169, 4.174, # moderatelow - Scenario C
3.88, 3.589, 3.2, # moderate - Scenario D
3.065, 2.544, 2.107) # greatest - Scenario E
reg <- c("GP", "hunt", "trap",
"GP", "hunt", "trap",
"GP", "hunt", "trap",
"GP", "hunt", "trap",
"GP", "hunt", "trap")
mydata <- data.frame(item, mean, reg)
I used the following code to generate the figure
ggplot(mydata, aes(x = item, y = mean, fill = reg)) +
ggtitle("How acceptable are each of the following scenarios to you?")+
coord_flip() +
geom_bar(position = "dodge", stat = "identity") +
# facet_wrap(~item, scales = "free_x") + # changed
scale_fill_manual(values=cbPalette) +
# scale_fill_grey(start = 0.8, end = 0.2) +
ylab("1 = highly unacceptable, 7 = highly acceptable") +
xlab("") +
theme_bw() +
#theme(legend.position="bottom")+
scale_x_discrete(waiver()) +
labs(fill="reg")
Here is the resulting figure:
ETA something for the millionth time - I figured out what works for me, which is using
ggtitle("How acceptable are each of the following scenarios to you?")+
coord_flip() +
geom_bar(position = "dodge", stat = "identity") +
# facet_wrap(~item, scales = "free_x") + # changed
scale_fill_manual(values=cbPalette) +
# scale_fill_grey(start = 0.8, end = 0.2) +
ylab("1 = highly unacceptable, 7 = highly acceptable") +
xlab("") +
theme_bw() +
#theme(legend.position="bottom")+
scale_x_discrete(breaks=c("none", "low", "moderatelow", "moderate", "greatest"),
labels=c("Control", "Treat 1", "Treat 2", "slkdj", "adkljf")) +
labs(fill="reg")
Thank you so much to those of you who commented! Your help led me to the answer.
ETA - okay here I am, back again. It was pointed out to me by #Gregor Thomas that my scale limits are set incorrectly, along with the unnecessary nature of some of my code. This feedback is much appreciated. Using the guidance of commentors, I was able to resolve the labeling issue that existed.
But, now I cannot figure out how to adjust the limits of the axis in the new code format. Given the following, how can I set the scale from 1-7 to reflect the nature of the likert scale people responded to? See code below.
ggplot(mydata, aes(y = item, x = mean, fill = reg)) +
geom_col(position = "dodge") +
scale_fill_manual(values = cbPalette) +
scale_y_discrete(breaks=c("none", "low", "moderatelow", "moderate", "greatest"),
labels=c("No wolves", "Very low numbers of wolves", "Moderately low numbers of wolves", "Moderate numbers of wolves", "Greatest numbers of wolves that can be sustained")
) +
scale_x_continuous(expand = expansion(mult = c(0, .05))) +
labs(
title = "How acceptable are each of the following scenarios to you?",
x = "1 = highly unacceptable, 7 = highly acceptable",
y = "",
fill = "population"
) +
theme_bw() +
theme(
legend.position = "bottom",
panel.grid.major.y = element_blank()
)
Here's how I'd clean up your code. I skip the coord_flip, just mapping the x and y variables as desired. I consolidate all the labels into labs(), and I use scale_y_discrete(labels = ) for the labels.
my_labels = rev(paste("Scenario", LETTERS[1:5]))
ggplot(mydata, aes(y = item, x = mean, fill = reg)) +
geom_col(position = "dodge") +
scale_fill_manual(values = cbPalette) +
scale_y_discrete(
labels = my_labels
) +
labs(
title = "How acceptable are each of the following scenarios to you?",
x = "1 = highly unacceptable, 7 = highly acceptable",
y = "",
fill = "population"
) +
theme_bw() +
theme(legend.position = "bottom")
If this were my plot, I'd adjust the x-scale to remove the padding below 0, and I'd remove the y gridlines, like this:
ggplot(mydata, aes(y = item, x = mean, fill = reg)) +
geom_col(position = "dodge") +
scale_fill_manual(values = cbPalette) +
scale_y_discrete(
labels = my_labels
) +
scale_x_continuous(expand = expansion(mult = c(0, .05))) +
labs(
title = "How acceptable are each of the following scenarios to you?",
x = "1 = highly unacceptable, 7 = highly acceptable",
y = "",
fill = "population"
) +
theme_bw() +
theme(
legend.position = "bottom",
panel.grid.major.y = element_blank()
)
Though if 1 is "highly unacceptable", I don't know how to interpret 0... the whole x scale is seems confusing. Maybe you should set the x limits to be from 1 to 7, not 0 to max of data (which is 5)? If so, use scale_x_continuous(expand = expansion(mult = c(0, .05)), limits = c(1, 7)).
We could add facet_grid(item ~ ., scales="free_y", space="free_y", switch="y")
ggplot(mydata, aes(x = item, y = mean, fill = reg)) +
ggtitle("How acceptable are each of the following scenarios to you?")+
coord_flip() +
geom_bar(position = "dodge", stat = "identity") +
# facet_wrap(~item, scales = "free_x") + # changed
scale_fill_manual(values=cbPalette) +
# scale_fill_grey(start = 0.8, end = 0.2) +
ylab("1 = highly unacceptable, 7 = highly acceptable") +
xlab("") +
theme_bw() +
theme(legend.position="bottom")+
scale_x_discrete(breaks = NULL) +
labs(fill="population") +
facet_grid(item ~ ., scales="free_y", space="free_y", switch="y") +
guides(fill=FALSE)

How to use loop with geom_vline and facet_wrap?

I have data similar to the one I've created below:
set.seed(42)
dates <- seq.Date(as.Date("2012-08-01"), as.Date("2014-08-30"), "day")
n <- length(dates)
dat <- data.frame(date = dates,
category = rep(LETTERS[1:4], n/2),
daily_count = sample(18:100, n, replace=TRUE))
#following to be used for creating dotted lines; highlighting a certain point for each category
point_dates <- sample(seq.Date(as.Date("2012-08-01"), as.Date("2014-08-30"), "month"),4)
category_name <- list("A", "B", "C", "D")
I am creating a boxplot for each category using facet_wrap, and point_dates are important for me as they shows the point of interest in each boxplot. This is how I am creating the plot:
ggplot(dat) +
geom_boxplot(aes(y = daily_count,
x = yearmonth(date),
group = paste(yearmonth(date), category),
fill = category)) +
labs(x = 'Month & Year',
y = 'Count',
fill = "Category") +
theme_bw() +
theme(axis.text=element_text(size=10),
axis.title=element_text(size=10),
legend.position="none") +
geom_vline(xintercept = lubridate::ymd("2013-08-23"), linetype=1, colour="red", size = 0.5)+
sapply(point_dates[[1]], function(xint) geom_vline(data=filter(dat,
category==category_name[[1]]),aes(xintercept = xint),
linetype=3, colour="black", size = 1))+
sapply(point_dates[[2]], function(xint) geom_vline(data=filter(dat,
category==category_name[[2]]),aes(xintercept = xint),
linetype=3, colour="black", size = 1))+
sapply(point_dates[[3]], function(xint) geom_vline(data=filter(dat,
category==category_name[[3]]),aes(xintercept = xint),
linetype=3, colour="black", size = 1))+
sapply(point_dates[[4]], function(xint) geom_vline(data=filter(dat,
category==category_name[[4]]),aes(xintercept = xint),
linetype=3, colour="black", size = 1))+
facet_wrap(~category, nrow = 2)
And this is the output of the code:
The plot is being created just fine. My question is, is there any better way (loop may be?) that would help me get rid of writing sapply multiple times. Because the number of categories may change (increase/decrease), that would be to change the code everytime.
Any guidance please?
I'm not sure that this is the best way, but you could do all of them in one go using map2 from tidyr. This would save you time from having to write out individual sapply.
library(tidyverse)
ggplot(dat) +
geom_boxplot(aes(y = daily_count,
x = yearmonth(date),
group = paste(yearmonth(date), category),
fill = category)) +
labs(x = 'Month & Year',
y = 'Count',
fill = "Category") +
theme_bw() +
theme(axis.text=element_text(size=10),
axis.title=element_text(size=10),
legend.position="none") +
geom_vline(xintercept = lubridate::ymd("2013-08-23"),
linetype=1, colour="red", size = 0.5)+
map2(point_dates, category_name,
~geom_vline(data=filter(dat, category==.y),
aes(xintercept = .x),
linetype=3, colour="black", size = 1))+
facet_wrap(~category, nrow = 2)
You can use map() to iterate the calls to sapply():
ggplot(dat) +
geom_boxplot(aes(y = daily_count,
x = yearmonth(date),
group = paste(yearmonth(date), category),
fill = category)) +
labs(x = 'Month & Year',
y = 'Count',
fill = "Category") +
theme_bw() +
theme(axis.text=element_text(size=10),
axis.title=element_text(size=10),
legend.position="none") +
geom_vline(xintercept = lubridate::ymd("2013-08-23"), linetype=1, colour="red", size = 0.5)+
map(seq_along(unique(dat$category)), ~sapply(point_dates[[.]], function(xint) geom_vline(data=filter(dat,
category==category_name[[.]]),aes(xintercept = xint),
linetype=3, colour="black", size = 1))) +
facet_wrap(~category, nrow = 2)
If i got it correct, you have already defined the dates for each group. So make the first plot:
library(ggplot2)
library(tsibble)
g = ggplot(dat) +
geom_boxplot(aes(y = daily_count,
x = yearmonth(date),
group = paste(yearmonth(date), category),
fill = category)) +
labs(x = 'Month & Year',
y = 'Count',
fill = "Category") +
theme_bw() +
theme(axis.text=element_text(size=10),
axis.title=element_text(size=10),
legend.position="none") +
geom_vline(xintercept = lubridate::ymd("2013-08-23"), linetype=1, colour="red", size = 0.5)+
facet_wrap(~category, nrow = 2)
You just need to provide a new data frame and call geom_vline:
tmp = data.frame(category=unlist(category_name),date=point_dates)
g + geom_vline(data=tmp,aes(xintercept = date),
linetype=3, colour="black", size = 1)

Editing graph using ggpattern in R

I wrote some code to make a graph (both below)
p <- ggplot(for_plots, aes(x = factor(condition), y = conflict, fill = smoking_status)) +
stat_summary(fun = "mean", geom = "bar", position = "dodge") +
theme_classic() +
scale_fill_manual(labels = c("Smokers", "Ex"),
values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
scale_color_manual(labels = c("Smokers", "Ex"),
values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
labs(x = 'Condition', y = 'Conflict (AUC)') +
scale_x_discrete(labels = c('Animal','Smoking')) +
coord_cartesian(ylim=c(0,1.5)) +
scale_y_continuous(expand = c(0,0))
p +
stat_summary(fun.data = mean_se, geom = "errorbar", width = .08, position = position_dodge(0.9))
However, I recently read about 'ggpattern' and wondered if anyone could help me add some diagonal black lines to the yellow bars in my plot (e.g. ex-smokers conflict). I have tried multiple ways, but adding 'geom_col_pattern' to the code seems to mess up the Y axis and provide overall conflict for each condition (animal, smoking) rather than separately for smokers and ex-smokers. I think the 'geom_col_pattern' perhaps is not compatible with the 'stat_summary' I have in my code. Does anyone have any suggestions?
Thank you
Instead of adding a geom_col_pattern on top of your plot, just update the geom argument of stat_summary.
#replicate of your dataframe
for_plots <- data.frame(matrix(nrow = 100, ncol=0))
for_plots$condition <- sample(rep(c("Animal", "Smoking"), 100), 100)
for_plots$smoking_status <- sample(rep(c("Smokers", "Ex"), 100), 100)
n_smoking <- length(which(for_plots$condition == "Smoking"))
for_plots$conflict[for_plots$condition=="Smoking"] <- sample(seq(0.8, 1.3, length.out = n_smoking), n_smoking)
n_animal <- length(which(for_plots$condition == "Animal"))
for_plots$conflict[for_plots$condition=="Animal"] <- sample(seq(0.5, 1, length.out = n_animal), n_animal)
p <- ggplot(for_plots, aes(x = factor(condition), y = conflict, fill = smoking_status)) +
stat_summary(aes(pattern=smoking_status),
fun = "mean", position = "dodge",
geom = "bar_pattern", pattern_fill="black", colour="black") + #edited part
theme_classic() +
scale_fill_manual(labels = c("Smokers", "Ex"),
values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
scale_color_manual(labels = c("Smokers", "Ex"),
values = c("blue", "gold"), guide = "legend", (title = "Smoking status")) +
labs(x = 'Condition', y = 'Conflict (AUC)') +
scale_pattern_manual(values=c("none", "stripe"))+ #edited part
scale_x_discrete(labels = c('Animal','Smoking')) +
coord_cartesian(ylim=c(0,1.5)) +
scale_y_continuous(expand = c(0,0))
p +
stat_summary(fun.data = mean_se, geom = "errorbar", width = .08, position = position_dodge(0.9))

Manually changing line size in ggplot

Still getting to grips with ggplot. My question: How do I manually change the line size? I've tried with scale_size_manual but it didn't seem to work.
setup:
test.mat <- data.frame(matrix(nrow=32, ncol =3))
test.mat[,1] = rep(1:16,2)
test.mat[1:16,2] = as.character(rep("Cohort Alpha"),16)
test.mat[17:32,2] = as.character(rep("Factor Alpha"), 16)
test.mat[,3] = rnorm(32,0,1)
colnames(test.mat) = c("Window", "type", "value")
ggplot(test.mat, aes(x=Window, y=value)) +
geom_line(aes(colour = type, linetype = type)) +
theme_classic() +
scale_colour_manual("type", values = c("black", "steelblue")) +
scale_linetype_manual("type", values = c("solid", "solid")) +
scale_size_manual("type", values = c(5, 1.4), guide = "none")
specify size inside aes() function as follows:
ggplot(test.mat, aes(x=Window, y=value)) +
geom_line(aes(colour = type, linetype = type, size = type)) +
theme_classic() +
scale_colour_manual("type", values = c("black", "steelblue")) +
scale_linetype_manual("type", values = c("solid", "solid")) +
scale_size_manual("type", values = c(5, 1.4), guide = "none")
Just turning #NelsonGon comment into an answer.
Is this what you want?
test.mat <- data.frame(matrix(nrow=32, ncol =3))
test.mat[,1] = rep(1:16,2)
test.mat[1:16,2] = as.character(rep("Cohort Alpha"),16)
test.mat[17:32,2] = as.character(rep("Factor Alpha"), 16)
test.mat[,3] = rnorm(32,0,1)
colnames(test.mat) = c("Window", "type", "value")
# -------------------------------------------------------------------------
base <- ggplot(test.mat, aes(x=Window, y=value))
#Here is where you need to add size
line_size <- base + geom_line(aes(colour = type, linetype = type), size=3)
line_size + theme_classic() +
scale_colour_manual("type", values = c("black", "steelblue")) +
scale_linetype_manual("type", values = c("solid", "solid")) +
scale_size_manual("type", values = c(5, 1.4), guide = "none")
output
Update
If you want variable thickness for the individual lines, you can do as follows.
base <- ggplot(test.mat, aes(x=Window, y=value))
#Use an ifelse to add variable thickness
line_size <- base + geom_line(aes(colour = type, size=ifelse(type=="Cohort Alpha",1,2)))
line_size + guides(size = FALSE)
To follow up on my comment on deepseefan's answer
base +
geom_line(aes(colour = type,
size=factor(ifelse(type=="Cohort Alpha", "thick", "thin"),
levels=c("thick","thin")))) +
scale_colour_manual(values = c("black", "steelblue")) +
scale_size_manual(values = c(5, 1.4), guide = FALSE)

How can I stop boxplot from shifting right when I use geom_text?

I am creating a single boxplot using ggplot, and everything is fine until I use geom_text to add label to horizontal line.
This is the code I am using without geom_text and the corresponding output:
ggplot(data = x_frame, aes(x = '', y = x_frame)) +
scale_y_log10(labels = comma, name='Transaction (GBP)') +
geom_boxplot() +
ggtitle('Box & Whisker')+
geom_hline(aes(yintercept = SE, colour = 'Tukey'), linetype='dashed') +
geom_hline(aes(yintercept = mean(x),colour = 'Mean'),linetype='dashed') +
geom_hline(aes(yintercept = mean_std,colour = 'Mean & 1Std'),linetype='dashed')+
scale_colour_manual(name = 'Thresholds', values=c(Tukey='red', Mean='blue', 'Mean & 1Std'='darkred'))+
guides(colour = guide_legend(override.aes = list(shape = c('Tukey','Mean','Mean & 1Std'), size = 3)))
And here is the code after using geom_text and the output:
ggplot(data = x_frame, aes(x = '', y = x_frame)) +
scale_y_log10(labels = comma, name='Transaction (GBP)') +
geom_boxplot() +
ggtitle('Box & Whisker')+
geom_hline(aes(yintercept = SE, colour = 'Tukey'), linetype='dashed') +
geom_hline(aes(yintercept = mean(x),colour = 'Mean'),linetype='dashed') +
geom_hline(aes(yintercept = mean_std,colour = 'Mean & 1Std'),linetype='dashed')+
scale_colour_manual(name = 'Thresholds', values=c(Tukey='red', Mean='blue', 'Mean & 1Std'='darkred'))+
geom_text(aes(y=SE, label=prettyNum(round(SE),big.mark=","), x=0.1), colour='red', hjust=-0.1 , vjust = 1) +
geom_text(aes(y=mean(x), label=prettyNum(round(mean(x)),big.mark=","), x=0.1), colour='blue', hjust=-0.1 , vjust = -0.5 ) +
geom_text(aes(y=mean_std, label=prettyNum(round(mean_std),big.mark=","), x=0.1), colour='darkred', hjust=-0.1 , vjust = -0.5) +
guides(colour = guide_legend(override.aes = list(shape = c('Tukey','Mean','Mean & 1Std'), size = 3)))
I am sorry I cannot post the data, but simply it is a vector of numbers with high standard deviation (hence, why I am using the log transformation).
Many thanks

Resources