Plotting my data in R with ggplot, the error bar whiskers are not displayed. Why are the whiskers not displayed and what is the fix so they will be displayed?
(Though it is not necessary to specify "data=..." in geom_errorbar, I am using a smaller set of data to plot a few points and their error bars on top of a larger set of data. I simplified here to just use the smaller data frame for everything but want to keep this example close to the code I intend to use.)
Thanks!
Shawna
shapes <- c(1, 19, 15, 1, 0)
names(shapes) <- levels(smallDF$Treatment)
p <- ggplot(data=smallDF, aes(x=pNew, y=diff, group=Treatment))
p <- p + geom_errorbar(data=smallDF, aes(ymin=diff-se,ymax=diff+se),
color="black", width=.3, position=position_dodge(.5))
p <- p + geom_line(size=.3)
p <- p + geom_point(data=smallDF, aes(shape=Treatment),fill="white",
size=2.5)
#p <- p + scale_shape_manual(values=c(1, 19, 15, 1, 0))
p <- p + scale_shape_manual(values=shapes)
p <- p + xlab("Pressure (mmHg)") + ylab("delD (mm)")
p <- p + theme_bw()
p <- p + theme(
legend.position="none"
# , axis.text.y=element_blank()
# , axis.title.y=element_blank()
, panel.border=element_blank()
, axis.line = element_line(colour = "black")
, axis.text.x = element_text(size=10)
, axis.text.y = element_text(size=10)
, axis.title.x= element_text(size=10)
, axis.title.y= element_text(size=10)
, strip.text.x = element_text(size = 10)
)
p
Treatment step N diff sd se ci predictD pNew
cntl 2 7 0.256537749 0.130605763 0.049364339 0.120790185 1.483185156 10
cntl 3 7 0.317586245 0.151444256 0.057240549 0.140062577 1.626590815 15
cntl 4 7 0.377309785 0.165262839 0.062463482 0.152842634 1.788401781 20
cntl 5 7 0.433531627 0.173735352 0.065665791 0.160678402 1.964393744 25
cntl 6 7 0.467529177 0.171603123 0.064859884 0.158706419 2.142879696 30
cntl 7 7 0.441401156 0.163740786 0.0618882 0.15143497 2.291729181 35
cntl 8 7 0.360578168 0.143967074 0.054414439 0.133147337 2.402152991 40
cntl 9 7 0.263484929 0.117425017 0.044382485 0.108600028 2.481824239 45
cntl 10 7 0.172079736 0.094209661 0.035607905 0.087129405 2.53504158 50
cntl 11 7 0.081780331 0.070316765 0.026577239 0.065032161 2.561500546 55
cntl 12 7 0.172079736 0.094209661 0.035607905 0.087129405 2.53504158 50
cntl 13 7 0.263484929 0.117425017 0.044382485 0.108600028 2.481824239 45
cntl 14 7 0.360578168 0.143967074 0.054414439 0.133147337 2.402152991 40
cntl 15 7 0.441401156 0.163740786 0.0618882 0.15143497 2.291729181 35
cntl 16 7 0.467529177 0.171603123 0.064859884 0.158706419 2.142879696 30
cntl 17 7 0.433531627 0.173735352 0.065665791 0.160678402 1.964393744 25
cntl 18 7 0.377309785 0.165262839 0.062463482 0.152842634 1.788401781 20
cntl 19 7 0.317586245 0.151444256 0.057240549 0.140062577 1.626590815 15
cntl 20 7 0.256537749 0.130605763 0.049364339 0.120790185 1.483185156 10
3hpx 2 6 0.124643574 0.068765439 0.028073373 0.072164903 1.511618688 10
3hpx 3 8 0.121806932 0.088542241 0.03130441 0.074023166 1.537544183 15
3hpx 4 8 0.138107729 0.097228081 0.034375318 0.08128471 1.602344034 20
3hpx 5 8 0.149529 0.10158369 0.035915258 0.08492609 1.665227481 25
3hpx 6 8 0.157687817 0.101898303 0.036026491 0.085189113 1.724788023 30
3hpx 7 8 0.154261671 0.099724849 0.035258058 0.08337206 1.776475381 35
3hpx 8 8 0.140631071 0.091483297 0.03234423 0.07648195 1.819131367 40
3hpx 9 8 0.123241311 0.083112381 0.029384664 0.069483689 1.854326249 45
3hpx 10 8 0.103092334 0.075467614 0.026681831 0.063092504 1.882440312 50
3hpx 11 8 0.080332775 0.070303352 0.024855989 0.058775073 1.903587298 55
3hpx 12 8 0.103092334 0.075467614 0.026681831 0.063092504 1.882440312 50
3hpx 13 8 0.123241311 0.083112381 0.029384664 0.069483689 1.854326249 45
3hpx 14 8 0.140631071 0.091483297 0.03234423 0.07648195 1.819131367 40
3hpx 15 8 0.154261671 0.099724849 0.035258058 0.08337206 1.776475381 35
3hpx 16 8 0.157687817 0.101898303 0.036026491 0.085189113 1.724788023 30
3hpx 17 8 0.149529 0.10158369 0.035915258 0.08492609 1.665227481 25
3hpx 18 8 0.138107729 0.097228081 0.034375318 0.08128471 1.602344034 20
3hpx 19 8 0.121806932 0.088542241 0.03130441 0.074023166 1.537544183 15
3hpx 20 6 0.124643574 0.068765439 0.028073373 0.072164903 1.511618688 10
I changed your width = 0.3 to width = 5. I also deleted all the redundant data = smallDF that you had and replaced your individually-set font sizes with the base_size argument of theme_bw().
p <- ggplot(data = smallDF, aes(x = pNew, y = diff, group = Treatment)) +
geom_errorbar(aes(ymin = diff - se, ymax = diff + se),
color = "black",
width = 5, ## This is the width of the crossbars!
position = position_dodge(.5))
geom_line(size = .3)
geom_point(aes(shape = Treatment),
fill = "white", size = 2.5) +
scale_shape_manual(values = shapes) +
xlab("Pressure (mmHg)") +
ylab("delD (mm)") +
theme_bw(base_size = 10) +
theme(
legend.position="none"
, panel.border=element_blank()
, axis.line = element_line(colour = "black")
)
p
Related
This question already has an answer here:
Annotation on only the first facet of ggplot in R?
(1 answer)
Closed last month.
I want to add a few texts in one facet out of four facets in my ggplot.
I am using annotate function to add a text but it generates the text at a given location (x,y) in every facet. Because the data variables have different ranges of y in each facet, the texts are not coming at a desired location (x,y).
Please let me know what should be done. Thanks.
library(dplyr)
library(tidyr)
library(ggplot2)
df%>%
select(Date, Ca, Na, K, Mg)%>%
gather(var,value,-Date)%>%
ggplot(aes(as.Date(Date), value))+
geom_point()+
theme_bw()+
facet_wrap(~var,scales = 'free_y',ncol = 1)+
ylab(" (ppm) (ppm)
(ppm) (ppm)")+
facet_wrap(~var,scales = 'free_y',ncol = 1, strip.position = "right")+
geom_vline(aes(xintercept = as.Date("2021-04-28")), col = "red")+
geom_vline(aes(xintercept = as.Date("2021-04-28")), col = "red")+
geom_vline(aes(xintercept = as.Date("2021-04-29")), col = "red")+
theme(axis.title = element_text(face="bold"))+
theme(axis.text = element_text(face="bold"))+
xlab('Date')+
theme(axis.title.x = element_text(margin = margin(t = 10)))+
theme(axis.title.y = element_text(margin = margin(r = 10)))+
annotate("text", label = "E1", x = as.Date("2021-04-28"), y = 2.8)
This is the code I am using for the desired output. I want to name all the xintercept lines which is E1, E2, E3 (from left to right) on the top of xaxis i.e. above the first facet of variable Ca in the data. Any suggestions?
Here is a part of my data:
df <- read.table(text = "
Date Ca K Mg Na
2/18/2021 1 25 21 19
2/22/2021 2 26 22 20
2/26/2021 3 27 23 21
3/4/2021 4 28 5 22
3/6/2021 5 29 6 8
3/10/2021 6 30 7 9
3/13/2021 7 31 8 10
3/17/2021 8 32 9 11
3/20/2021 9 33 10 12
3/23/2021 10 34 11 13
3/27/2021 11 35 12 14
3/31/2021 12 36 13 15
4/3/2021 13 37 14 16
4/7/2021 14 38 15 17
4/10/2021 15 39 16 18
4/13/2021 16 40 17 19
4/16/2021 17 41 18 20
4/19/2021 8 42 19 21
4/22/2021 9 43 20 22
4/26/2021 0 44 21 23
4/28/2021 1 45 22 24
4/28/2021 2 46 23 25
4/28/2021 3 47 24 26
4/28/2021 5 48 25 27
4/29/2021 6 49 26 28
5/4/2021 7 50 27 29
5/7/2021 8 51 28 30
5/8/2021 9 1 29 31
5/10/2021 1 2 30 32
5/29/2021 3 17 43 45
5/31/2021 6 18 44 46
6/1/2021 4 19 45 47
6/2/2021 8 20 46 48
6/3/2021 2 21 47 49
6/7/2021 3 22 48 50
6/10/2021 5 23 49 51
6/14/2021 3 5 50 1
6/18/2021 1 6 51 2
", header = TRUE)
Prepare the data before plotting, make a separate data for text annotation:
dfplot <- df %>%
select(Date, Ca, Na, K, Mg) %>%
#convert to date class before plotting
mutate(Date = as.Date(Date, "%m/%d/%Y")) %>%
#using pivot instead of gather. gather is superseded.
#gather(var, value, -Date)
pivot_longer(cols = 2:5, names_to = "grp", values_to = "ppm")
dftext <- data.frame(grp = "Ca", # we want text to show up only on "Ca" facet.
ppm = max(dfplot[ dfplot$grp == "Ca", "ppm" ]),
Date = as.Date(c("2021-04-27", "2021-04-28", "2021-04-29")),
label = c("E1", "E2", "E3"))
After cleaning up your code, we can use geom_text with dftext:
ggplot(dfplot, aes(Date, ppm)) +
geom_point() +
facet_wrap(~grp, scales = 'free_y',ncol = 1, strip.position = "right") +
geom_vline(xintercept = dftext$Date, col = "red") +
geom_text(aes(x = Date, y = ppm, label = label), data = dftext, nudge_y = -2)
Try using ggrepel library to avoid label overlap, replace geom_text with one of these:
#geom_text_repel(aes(x = Date, y = ppm, label = label), data = dftext)
#geom_label_repel(aes(x = Date, y = ppm, label = label), data = dftext)
After cleaning up the code and seeing the plot, I think this post is a duplicate of Annotation on only the first facet of ggplot in R? .
I am trying to align all of my x-labels, where they are left justified, and start from the same point. In the code below, when I set hjust=-.01, it basically looks correct:
However, if I try to nudge it a bit further to the right, by setting hjust=-.05, everything falls out of alignment:
ggplot(dt.summ, aes(x=reorder(dialogue_act,n), y=n)) +
geom_col(aes(alpha=.3)) +
geom_text(aes(y=-.5, x=dialogue_act, label=dialogue_act), hjust=-.01, size=3) +
theme(axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank()) +
scale_y_continuous(expand = c(0, 0)) +
coord_flip()
How can I correct this?
Data:
> print(dt.summ, n=nrow(dt.summ))
# A tibble: 27 × 2
dialogue_act n
<chr> <int>
1 Statement-non-opinion 2650
2 Statement-opinion 666
3 Yes-No-Question 483
4 Wh-Question 255
5 Appreciation 211
6 Conventional-closing 107
7 Conventional-opening 83
8 Agree/Accept 77
9 Declarative Yes-No-Question 71
10 Acknowledge (Backchannel) 60
11 Open-Question 56
12 Action-directive 27
13 Repeat-phrase 22
14 Quotation 18
15 Collaborative Completion 16
16 Signal-non-understanding 13
17 Negative Non-no Answers 11
18 Backchannel in Question Form 8
19 No Answers 8
20 Apology 7
21 Hold Before Answer/Agreement 7
22 Or-Clause 6
23 Rhetorical-Question 6
24 Offers, Options Commits 4
25 Hedge 3
26 Other 2
27 Self-talk 2
Answered my own question. Changed hjust=0 and aes(y=100).
ggplot(dt.summ, aes(x=reorder(dialogue_act,n), y=n)) +
geom_col(aes(alpha=.3)) +
geom_text(aes(y=100, x=dialogue_act, label=dialogue_act), hjust=0, size=3) +
theme(axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank()) +
scale_y_continuous(expand = c(0, 0)) +
coord_flip()
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)
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)
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))