Add a percent to y axis labels [duplicate] - r

This question already has answers here:
How can I change the Y-axis figures into percentages in a barplot?
(4 answers)
Closed 4 years ago.
I'm sure I missed an obvious solution tot his problem but I can't figure out how to add a percent sign to the y axis labels.
Data Sample:
Provider Month Total_Count Total_Visits Procedures RX State
Roberts 2 19 19 0 0 IL
Allen 2 85 81 4 4 IL
Dawson 2 34 34 0 0 CA
Engle 2 104 100 4 4 CA
Goldbloom 2 7 6 1 1 NM
Nathan 2 221 192 29 20 NM
Castro 2 6 6 0 0 AK
Sherwin 2 24 24 0 0 AK
Brown 2 282 270 12 12 UT
Jackson 2 114 96 18 16 UT
Corwin 2 22 22 0 0 CO
Dorris 2 124 102 22 22 CO
Ferris 2 427 318 109 108 OH
Jeffries 2 319 237 82 67 OH
The following code gives graphs with inaccurate values because R seems to be multiplying by 100.
procs <- read.csv(paste0(dirdata, "Procedure percents Feb.csv"))
procs$Percentage <- round(procs$Procedures/procs$Total.Visits*100, 2)
procs$Percentage[is.na(procs$Percentage)] <- 0
procsplit <- split(procs, procs$State)
plots <- function(procs) {
ggplot(data = procs, aes(x= Provider, y= Percentage, fill= Percentage)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(x = Provider, y = Percentage, label = sprintf("%.1f%%", Percentage)), position = position_dodge(width = 0.9), hjust = .5, vjust = 0 , angle = 0) +
theme(axis.text.x = element_text(angle = 45, vjust = .5)) +
ggtitle("Procedure Percentages- February 2018", procs$State) +
theme(plot.title = element_text(size = 22, hjust = .5, family = "serif")) +
theme(plot.subtitle = element_text(size = 18, hjust = .5, family = "serif")) +
scale_y_continuous(name = "Percentage", labels = percent)
}
lapply(procsplit, plots)
I'm not sure if there's a way to use sprintf to add it or if there's a way to paste it onto the labels.

adding + scale_y_continuous(labels = function(x) paste0(x, "%")) to the ggplot statement fixes this issue

Related

Geom_spoke not plotting properly

I am trying to plot the length and angle of a bursting event for a marine animal. However, I dont think it is plotting properly. The event occurring at 75m has an ascent of 31 however this is not translating to the graph.
Further, I was wanting to cut the x-axis between 75 & 95.
ggplot(depth, aes(y=hour, x= Startdepth,)) +
geom_point(size = 3) +
geom_spoke(aes(angle = Pitchavg, radius = ascent), size = 1) +
theme_classic() +
scale_color_manual(values = c("#E7B800","#FC4E07", "#00AFBB", '#004E54')) +
annotate("rect", ymin = 5, ymax = 8.5, xmin = -1, xmax = 100,
alpha = .1, fill = "blue") +
annotate("rect", ymin = 16.5, ymax = 21.5, xmin = -1, xmax =100,
alpha = .1, fill = "blue") +
ylim(0, 24)
Below is a sample of my data:
Shark Startdepth Pitchavg ascent Hour
3 5.84887 15.170846 3.34844 19
4 0.424778 -1.737293333 0.014004 22
5 4.1233 7.2578775 0.02645 19
5 4.95108 17.91744 1.07518 19
5 4.77525 23.388734 0.95225 20
6 5.57968 -20.51986667 0 22
6 10.3285 37.778 4.48275 0
6 6.73732 17.5535036 5.52834 1
6 7.01584 27.8316 2.28104 1
6 0.977145 -24.41713333 0 4
6 55.3021 31.6784 9.9084 15
7 8.04433 24.8095 0 10
7 18.712 45.95876667 0 10
7 17.3225 26.36621125 16.374918 10
7 14.0488 37.84178571 0 11
7 20.24 35.13114667 0 11
7 25.5022 9.78095025 2.4459 13
The problem seems to be that geom_spoke accepts angle in radians and you have angles in degrees. Convert radians to degrees in aes and the plot seems right.
I have commented out the scale_color_* since the variable mapping to color was removed from the data, as said in the OP's comment.
x <- 'Shark Startdepth Pitchavg ascent Hour
3 5.84887 15.170846 3.34844 19
4 0.424778 -1.737293333 0.014004 22
5 4.1233 7.2578775 0.02645 19
5 4.95108 17.91744 1.07518 19
5 4.77525 23.388734 0.95225 20
6 5.57968 -20.51986667 0 22
6 10.3285 37.778 4.48275 0
6 6.73732 17.5535036 5.52834 1
6 7.01584 27.8316 2.28104 1
6 0.977145 -24.41713333 0 4
6 55.3021 31.6784 9.9084 15
7 8.04433 24.8095 0 10
7 18.712 45.95876667 0 10
7 17.3225 26.36621125 16.374918 10
7 14.0488 37.84178571 0 11
7 20.24 35.13114667 0 11
7 25.5022 9.78095025 2.4459 13'
depth <- read.table(textConnection(x), header = TRUE)
library(ggplot2)
ggplot(depth, aes(x = Startdepth, y = Hour)) +
geom_point(size = 3) +
geom_spoke(aes(angle = Pitchavg * pi/180, radius = ascent), size = 1) +
#scale_color_manual(values = c("#E7B800","#FC4E07", "#00AFBB", '#004E54')) +
annotate("rect", ymin = c(5, 16.5), ymax = c(8.5, 21.5), xmin = c(-1, -1), xmax = c(100, 100),
alpha = 0.1, fill = "blue") +
ylim(0, 24) +
theme_classic()
Created on 2022-05-12 by the reprex package (v2.0.1)

ggplot2: geom_bar(); how to alternate order of fill so bars are not lost inside a bar with a higher value?

I am trying to position two bars at the same position on the x-axis and seperated out by colour (almost as if stacking).
However, instead of stacking I want the bar simply inside the other bar - with the smallest Y-value being visable inside the bar with the highest Y-value.
I can get this to work to some extent - but the issue is that one Y-value is not consistently higher across one of the two factors. This leads to bars being 'lost' within a bar with a higher Y-value.
Here is a subset of my dataset and the current ggplot code:
condition hours expression freq_genes
1 tofde 9 up 27
2 tofde 12 up 92
3 tofde 15 up 628
17 tofde 9 down 0
18 tofde 12 down 1
19 tofde 15 down 0
33 tofp 9 up 2462
34 tofp 12 up 786
35 tofp 15 up 298
49 tofp 9 down 651
50 tofp 12 down 982
51 tofp 15 down 1034
65 tos 0 up 27
66 tos 3 up 123
67 tos 6 up 752
81 tos 0 down 1
82 tos 3 down 98
83 tos 6 down 594
sf_plot <- ggplot(data = gene_freq,
aes(x = hours,
y = freq_genes,
group = condition,
fill = factor(expression,
labels=c("Down",
"Up"))))
sf_plot <- sf_plot + labs(fill="Expression")
sf_plot <- sf_plot + geom_bar(stat = "identity",
width = 2.5,
position = "dodge")
sf_plot <- sf_plot + scale_fill_manual(values=c("#9ecae1",
"#3182bd"))
sf_plot <- sf_plot + xlab("Time (Hours)")
sf_plot <- sf_plot + scale_x_continuous(breaks =
seq(min(gene_freq$freq_genes),
max(gene_freq$freq_genes),
by = 3))
sf_plot <- sf_plot + ylab("Gene Frequency")
sf_plot <- sf_plot + facet_grid(. ~ condition, scales = "free")
sf_plot <- sf_plot + theme_bw()
sf_plot <- sf_plot + theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
sf_plot <- sf_plot + theme(axis.text.x = element_text(angle = 90))
# Print plot
sf_plot
You can add alpha = 0.5 to your geom_bar() statement to make the bars transparent. This will allow both bars to be seen. Adding that alpha statement and nothing else will produce what you're looking for, to make both overlaid bars visible. The colors, however, make seeing the two different bars challenging.
Another (and maybe better) option is to change the order in which the plot is created. If I recall correctly, ggplot will plot the bars in alphabetical or numeric or factor-level order. Here, your expression values are c("Down", "Up") and "Down" is being plotted first. If you force "Up" to be plotted first, you could resolve this, too.
library(dplyr)
library(ggplot2)
dat <-
read.table(text = "condition hours expression freq_genes
1 tofde 9 up 27
2 tofde 12 up 92
3 tofde 15 up 628
17 tofde 9 down 0
18 tofde 12 down 1
19 tofde 15 down 0
33 tofp 9 up 2462
34 tofp 12 up 786
35 tofp 15 up 298
49 tofp 9 down 651
50 tofp 12 down 982
51 tofp 15 down 1034
65 tos 0 up 27
66 tos 3 up 123
67 tos 6 up 752
81 tos 0 down 1
82 tos 3 down 98
83 tos 6 down 594") %>%
mutate(expression2 = ifelse(expression == "up", 1, 2))
dat %>%
ggplot(aes(x = hours, y = freq_genes, group = condition,
fill = factor(expression2, labels=c("Up", "Down")))) +
labs(fill="Expression") +
geom_bar(stat = "identity", position = "dodge", width = 2.5, alpha = 0.5) +
scale_fill_manual(values=c("#9ecae1", "#3182bd")) +
xlab("Time (Hours)") +
scale_x_continuous(breaks = seq(min(dat$freq_genes),
max(dat$freq_genes),
by = 3)) +
ylab("Gene Frequency") +
facet_grid(. ~ condition, scales = "free") +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "bottom",
axis.text.x = element_text(angle = 90))
Here, I've created a new column called expression2 that is just a numeric version of expression. I changed the fill variable in aes() to match with those new labels. I left the colors in scale_fill_manual() the same as in your original statement and kept the alpha value. "Down" is being plotted on top of "Up" but in keeping the same colors with the alpha value, both bars are easier to see. You can play with the legend to display "Down" before "Up" if that's necessary.
Note that providing machine readable data goes a long way in allowing others to help you out. Consider using dput() to output your data next time rather than pasting it in. Also note that you can "chain" together ggplot() statements with a +. This makes code much more compact and easier to read.

Grouped boxplot combined with dotplot in ggplot2

I am new to stackoverflow so please be forgiving, if the way of asking my question can be improved (in that case I'm happy if you let me know how).
I am using the following code in ggplot2 to produce a grouped boxplot combined with a dotplot (unfortunately I cannot post an image yet (no reputation)):
ggplot(data24, aes(x=intensity, y=percacc, fill=group)) +
geom_boxplot(position=position_dodge(1), notch=T,
outlier.colour = NA, width = .7, alpha=0.2) +
geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 3,
position=position_dodge(1), dotsize=0.5, alpha=1) +
stat_summary(fun.y=mean, geom="point", shape=23, size=5,
position=position_dodge(0.3)) +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.1,
position=position_dodge(0.3), size=1.2)+
scale_y_continuous(limits=c(0,100)) +
scale_fill_discrete(name="Group")
My questions are:
How can use different colors for different elements? I tried to add color/fill commands within geom_boxplot() and geom_dotplot, but this doesn't work: e.g., if I add fill="green" to geom_dotplot(), all points become green and centered between the boxplots. How can I rewrite the code to get
white fill for all boxplots
blue fill and black line for dots of group 1
green fill and black line for dots of group 2
black fill for all mean diamonds
How can pull the categories of "intensity" (i.e. the three pairs of boxplots) further apart from each other?
How can I display the dotplots next to the boxplots and the mean+CI in the boxplot?
Why does my coordination system still go from <0 to >100, even if I defined the y-axis to go from 0 to 100?
Thank you!
Edit 180722
Thank you for your comments. The data head is:
id intensity AQ_sum group acc percacc
1 54 40 11 COMP 5 20.83333
2 54 60 11 COMP 18 75.00000
3 54 80 11 COMP 24 100.00000
4 55 40 12 COMP 9 37.50000
5 55 60 12 COMP 22 91.66667
6 55 80 12 COMP 24 100.00000
7 58 40 10 COMP 8 33.33333
8 58 60 10 COMP 22 91.66667
9 58 80 10 COMP 23 95.83333
10 59 40 6 COMP 19 79.16667
11 59 60 6 COMP 24 100.00000
12 59 80 6 COMP 24 100.00000
13 60 40 9 COMP 10 41.66667
14 60 60 9 COMP 23 95.83333
15 60 80 9 COMP 22 91.66667
16 61 40 13 COMP 4 16.66667
17 61 60 13 COMP 19 79.16667
18 61 80 13 COMP 24 100.00000
19 62 40 12 COMP 16 66.66667
20 62 60 12 COMP 23 95.83333
My updated code is
ggplot(data24, aes(x=intensity, y=percacc, fill=group)) +
geom_boxplot(position=position_dodge(0.8), notch=T,
outlier.colour = NA, width = .4, alpha=0.3) +
geom_dotplot(binaxis = "y", stackdir = "center", binwidth = 3,
position=position_dodge(0.8), dotsize=0.4, alpha=1)+
scale_fill_manual(values=c("#999999", "#E69F00"), name="Group") +
stat_summary(fun.y=mean, geom="point", shape=23, size=3,
position=position_dodge(0.2)) +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.1,
position=position_dodge(0.2), size=0.5)+
scale_y_continuous(limits=c(0,103), expand = c(0, 0),
breaks=c(0,20,40,60,80,100), name="Percentage accuracy") +
scale_x_discrete(expand = c(0, 0.6), name="Degree of intensity (in percent)") +
labs(title="Accuracy by intensity and group\n") +
theme_light()+
theme(plot.title = element_text(face='bold', size=12, hjust = 0.5),
axis.title.x = element_text(size=10,hjust=0.5),
axis.title.y = element_text(size=10,vjust=1),
axis.text.x = element_text(size=10,color='black'),
axis.text.y = element_text(size=10, color='black'),
panel.grid.major.y = element_line(size = 0.3, linetype = "dotted", color="darkgrey"),
panel.grid.minor.y = element_line(size = 0.3, linetype = "dotted", color="darkgrey"),
panel.grid.major.x = element_blank(),
panel.border = element_blank(),
axis.line = element_line(size = 0.5, linetype = "solid", colour = "black")) +
ggsave("plotintensity.png", width = 10, height = 5)

How to add mean separation letters above bars in a vertical facet?

I'm trying to make a figure containing three bar plots vertically. I can do it successfully using Facets in ggplot2 but was wondering how I can add the mean separation letters above each bar for the respective plot. Data is separated by harvest in the facet (1-3). Please find sample data below, followed by code for the facet, and then final code I created for just one of the harvest in which I was successful in adding separation letters. Thank you:
Rootstock Harvest N Length.cm. sd se ci Lower Upper
1 A 1 8 76.51075 36.00585368 12.72999165 30.10164697 63.78075835 89.24074165
2 A 2 8 160.9014 59.41736724 21.00721165 49.67416212 139.8941884 181.9086116
3 A 3 8 325.2713625 117.812772 41.653105 98.49394223 283.6182575 366.9244675
4 B 1 8 71.09765 38.1718855 13.49579954 31.91249489 57.60185046 84.59344954
5 B 2 8 142.1246375 67.33487775 23.80647433 56.29336655 118.3181632 165.9311118
6 B 3 8 336.24105 115.3372241 40.77786664 96.42433239 295.4631834 377.0189166
7 C 1 8 46.3798 25.53625659 9.028430101 21.34884477 37.3513699 55.4082301
8 C 2 8 118.4134875 65.35360032 23.10598698 54.63697717 95.30750052 141.5194745
9 C 3 8 281.5792375 117.049729 41.38332856 97.85602232 240.1959089 322.9625661
10 D 1 7 86.62565714 37.50832694 14.17681503 34.6894167 72.44884212 100.8024722
11 D 2 6 120.86055 48.8806253 19.95543172 51.2970703 100.9051183 140.8159817
12 D 3 8 274.203525 86.86426453 30.71115524 72.62034249 243.4923698 304.9146802
13 E 1 8 57.2146875 26.41120188 9.337769974 22.08031734 47.87691753 66.55245747
14 E 2 8 120.2972875 45.39353788 16.04903923 37.94994738 104.2482483 136.3463267
15 E 3 8 333.678325 103.5042683 36.594285 86.53173379 297.08404 370.27261
16 F 1 8 81.166225 37.18034784 13.14523804 31.08354867 68.02098696 94.31146304
17 F 2 8 161.96095 71.60747702 25.31706629 59.86534893 136.6438837 187.2780163
18 F 3 7 331.6084286 195.9165314 74.04948854 181.1925711 257.55894 405.6579171
19 G 1 8 68.743 44.9696775 15.89918195 37.59559123 52.84381805 84.64218195
20 G 2 8 142.6372625 80.95226642 28.62094827 67.67778838 114.0163142 171.2582108
21 G 3 8 362.7983625 143.4288665 50.70976205 119.9095331 312.0886004 413.5081246
22 H 1 8 55.2445625 31.35474448 11.08557622 26.21322238 44.15898628 66.33013872
23 H 2 8 125.48165 97.10938809 34.33335342 81.18548013 91.14829658 159.8150034
24 H 3 8 402.004975 176.9811861 62.57229841 147.9599743 339.4326766 464.5772734
25 I 1 7 63.47242857 30.17893187 11.40656408 27.91085683 52.06586449 74.87899265
26 I 2 8 167.4317875 76.30008692 26.97615443 63.78846899 140.4556331 194.4079419
27 I 3 8 357.2169125 108.6952357 38.42956911 90.8714911 318.7873434 395.6464816
28 J 1 8 65.6122875 29.5111406 10.43376382 24.67193096 55.17852368 76.04605132
29 J 2 8 126.4079875 66.06781069 23.35849848 55.23407198 103.049489 149.766486
30 J 3 8 502.1878625 140.3556285 49.62320833 117.3402419 452.5646542 551.8110708
31 K 1 8 57.3303625 27.86618278 9.852183406 23.29671181 47.47817909 67.18254591
32 K 2 8 106.203025 52.69142229 18.629231 44.05113142 87.573794 124.832256
33 K 3 7 348.9141 166.4786335 62.92300897 153.9670564 285.991091 411.837109
34 L 1 8 54.4373375 32.89032536 11.62848605 27.49700012 42.80885145 66.06582355
35 L 2 8 173.801975 114.7929316 40.58543019 95.9692925 133.2165448 214.3874052
36 L 3 8 427.5621 141.5160672 50.03348538 118.3103929 377.5286146 477.5955854
37 N 1 8 53.19965 18.75340803 6.630330993 15.67824146 46.56931901 59.82998099
38 N 2 8 102.83935 49.66950295 17.56082118 41.52474363 85.27852882 120.4001712
39 N 3 8 361.586525 123.0931912 43.52001511 102.9084831 318.0665099 405.1065401
40 M 1 8 90.71195 45.58629605 16.11718953 38.11109723 74.59476047 106.8291395
41 M 2 8 201.219475 90.14563026 31.87129323 75.36363289 169.3481818 233.0907682
42 M 3 8 521.6052875 144.9878341 51.26094033 121.2128627 470.3443472 572.8662278
43 O 1 8 72.572875 37.76520057 13.35201471 31.57249778 59.22086029 85.92488971
44 O 2 8 181.783975 97.67988452 34.53505436 81.66242708 147.2489206 216.3190294
45 O 3 8 415.6914625 179.8261041 63.57812883 150.3383853 352.1133337 479.2695913
46 P 1 8 58.817375 22.82069356 8.068333583 19.07857726 50.74904142 66.88570858
47 P 2 8 131.3006875 67.37113752 23.8192941 56.32368048 107.4813934 155.1199816
48 P 3 8 286.8072625 49.36617664 17.45357913 41.27115649 269.3536834 304.2608416
49 Q 1 8 76.4929625 50.22446933 17.75703142 41.98870713 58.73593108 94.24999392
50 Q 2 8 174.730975 111.6447534 39.47238111 93.33734965 135.2585939 214.2033561
51 Q 3 8 363.4492125 169.6025304 59.96354967 141.7912638 303.4856628 423.4127622
52 R 1 8 72.9998875 24.48056866 8.655188055 20.46626758 64.34469945 81.65507555
53 R 2 8 172.1736375 97.8407617 34.59193304 81.79692377 137.5817045 206.7655705
54 R 3 8 453.68845 177.718465 62.83296587 148.5763549 390.8554841 516.5214159
Code with Facet:
library(ggplot2)
AllHarvest<-summarySE(CombinedwinrhizoSigmaPlot, measurevar="Length.cm.", groupvars=c("Rootstock","Harvest"))
AllHarvest$Lower <- AllHarvest$Length.cm. - AllHarvest$se
AllHarvest$Upper <- AllHarvest$Length.cm. + AllHarvest$se
AllHplot<-ggplot(AllHarvest,aes(x=Rootstock, y=Length.cm., fill=Rootstock)) + ylim(0,600) + geom_bar(colour = "black", stat="identity", width = 0.7, fill = "#4DAF4A") + geom_errorbar(aes(ymax=Upper, ymin=Lower, width = 0.2), size=0.5, color = "black")
AllHplot + facet_grid(Harvest ~ .)
Code for Harvest #3 including mean separation letters above bars:
Harvest3 <- AllHarvest[which(Harvest =="3"),]
Harvest3summary <- summarySE(Harvest3, measurevar="Length.cm.", groupvars=c("Rootstock"))
offset.v = -8 # offsets for mean letters
offset.h = 0.4
Harvest3summary$Lower <- Harvest3summary$Length.cm. - Harvest3summary$se
Harvest3summary$Upper <- Harvest3summary$Length.cm. + Harvest3summary$se
Harvest3plot <- ggplot(Harvest3summary,aes(x=Rootstock, y=Length.cm., fill=Rootstock)) + ylim(0,600) +
geom_bar(colour = "black", stat="identity", width = 0.7, fill = "#4DAF4A") + geom_errorbar(aes(ymax=Upper, ymin=Lower, width = 0.2), size=0.5, color = "black") +
geom_text(aes(label=c("CDE", "CDE", "DE", "DE","CDE","E","BCDE","ABCDE","ABCDE","AB","CDE", "ABC", "ABCDE","A","ABCD","CDE","BCDE","ABC"), size = 8, hjust=offset.h, vjust=offset.v)) +
labs(x = "", y = expression(Total~Root~Length~(cm))) + theme(legend.position = "none") +
## ggtitle("Main title") +
theme(
plot.title = element_text(size = rel(1.5),
face = "bold", vjust = 1.5),
axis.title = element_text(face = "bold"),
axis.title.y = element_text(vjust= 1.8, size=14),
axis.text.y=element_text(size=12),
axis.title.x = element_text(vjust= -0.5),
axis.text.x = element_text(angle=60, hjust=1.1,vjust=1.05, size=14, colour="black"))
panel.border = element_rect(colour="black")
After looking around a bit I was able to answer my own question through trial and error. I am adding code for those who may want to use it in the future - this is especially helpful for journal figures where mean separation letters are needed. A note - when using geom_text you add your separation letters as one long string for all graphs in the facet, so make sure you have them ordered correctly:
AllHplot<-ggplot(AllHarvest,aes(x=Rootstock, y=Length.cm., fill=Rootstock)) + coord_cartesian(ylim=c(0,650)) + geom_blank()+ theme_bw() + geom_bar(colour = "black", stat="identity", width = 0.7, fill = "black") + geom_errorbar(aes(ymax=Upper, ymin=Lower, width = 0.2), size=0.5, color = "black")+
labs(x = "", y = expression(paste(Total~Root~Length~(cm)))) + scale_y_continuous(expand = c(0,0)) +
theme(
axis.title = element_text(face = "bold"),
axis.title.y = element_text(face="bold", vjust= 1.8, size=14),
axis.text.y=element_text(size=12, colour="black"),
axis.title.x = element_text(vjust= -0.5),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
axis.text.x = element_text(angle=60, hjust=1.1, vjust=1.05, size=14, colour="black"),
strip.background = element_blank(),
strip.text = element_blank())+ geom_text(aes(label=c("CDE", "CDE", "DE", "DE","CDE","E","BCDE","ABCDE","ABCDE","AB","CDE", "ABC", "ABCDE","A","ABCD","CDE","BCDE","ABC", "ABC", "ABCDEF", "CDEF", "BCDEF","BCDEF","ABC","BCDEF","EF","ABC","CDEF","DEF", "ABCD", "F","A","AB","BCDEF","ABCDE","ABC", "ABC", "ABC", "D", "AB","BCD","AB","ABCD","CD","ABCD","ABCD","BCD", "CD", "BCD","A","ABC","ABCD","ABC","ABC"), hjust=offset.h, vjust=offset.v)) + facet_grid(Harvest ~ .,as.table=F)

ggplot2 merge color and fill legends

I want to merge two legends in ggplot2. I use the following code:
ggplot(dat_ribbon, aes(x = x)) +
geom_ribbon(aes(ymin = ymin, ymax = ymax,
group = group, fill = "test4 test5"), alpha = 0.2) +
geom_line(aes(y = y, color = "Test2"), data = dat_m) +
scale_colour_manual(values=c("Test2" = "white", "test"="black", "Test3"="red")) +
scale_fill_manual(values = c("test4 test5"= "dodgerblue4")) +
theme(legend.title=element_blank(),
legend.position = c(0.8, 0.85),
legend.background = element_rect(fill="transparent"),
legend.key = element_rect(colour = 'purple', size = 0.5))
The output is shown below. There are two problems:
When I use two or more words in the fill legend, the alignment becomes wrong
I want to merge the two legends into one, such that the fill legend is just part of a block of 4.
Does anyone know how I can achieve this?
Edit: reproducible data:
dat_m <- read.table(text="x quantile y group
1 1 50 0.4967335 0
2 2 50 0.4978249 0
3 3 50 0.5113562 0
4 4 50 0.4977866 0
5 5 50 0.5013287 0
6 6 50 0.4997994 0
7 7 50 0.4961121 0
8 8 50 0.4991302 0
9 9 50 0.4976087 0
10 10 50 0.5011666 0")
dat_ribbon <- read.table(text="
x ymin group ymax
1 1 0.09779713 40 0.8992385
2 2 0.09979283 40 0.8996875
3 3 0.10309222 40 0.9004759
4 4 0.10058433 40 0.8985366
5 5 0.10259125 40 0.9043807
6 6 0.09643109 40 0.9031940
7 7 0.10199870 40 0.9022920
8 8 0.10018253 40 0.8965690
9 9 0.10292754 40 0.9010934
10 10 0.09399359 40 0.9053067
11 1 0.20164694 30 0.7974174
12 2 0.20082056 30 0.7980642
13 3 0.20837821 30 0.8056074
14 4 0.19903399 30 0.7973723
15 5 0.19903322 30 0.8050146
16 6 0.19965049 30 0.8051922
17 7 0.20592719 30 0.8042850
18 8 0.19810139 30 0.7956606
19 9 0.20537392 30 0.8007527
20 10 0.19325158 30 0.8023044
21 1 0.30016463 20 0.6953927
22 2 0.29803646 20 0.6976961
23 3 0.30803808 20 0.7048137
24 4 0.30045448 20 0.6991248
25 5 0.29562249 20 0.7031225
26 6 0.29647060 20 0.7043499
27 7 0.30159103 20 0.6991356
28 8 0.30369025 20 0.6949053
29 9 0.30196483 20 0.6998127
30 10 0.29578036 20 0.7015861
31 1 0.40045725 10 0.5981147
32 2 0.39796299 10 0.5974115
33 3 0.41056038 10 0.6057062
34 4 0.40046287 10 0.5943157
35 5 0.39708008 10 0.6014512
36 6 0.39594129 10 0.6011162
37 7 0.40052411 10 0.5996186
38 8 0.40128517 10 0.5959748
39 9 0.39917658 10 0.6004600
40 10 0.39791453 10 0.5999168")
You are not using ggplot2 according to its philosophy. That makes things difficult.
ggplot(dat_ribbon, aes(x = x)) +
geom_ribbon(aes(ymin = ymin, ymax = ymax, group = group, fill = "test4 test5"),
alpha = 0.2) +
geom_line(aes(y = y, color = "Test2"), data = dat_m) +
geom_blank(data = data.frame(x = rep(5, 4), y = 0.5,
group = c("test4 test5", "Test2", "test", "Test3")),
aes(y = y, color = group, fill = group)) +
scale_color_manual(name = "combined legend",
values=c("test4 test5"= NA, "Test2" = "white",
"test"="black", "Test3"="red")) +
scale_fill_manual(name = "combined legend",
values = c("test4 test5"= "dodgerblue4",
"Test2" = NA, "test"=NA, "Test3"=NA))

Resources