Adding text in one of the four facets [duplicate] - r

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? .

Related

Remove link between time series and add minor date tick on x_axis in ggplot

I was trying to plot a time series composed of weekly averanges. Here is the plot that I have obtained:
[weekly averages A]
[1]: https://i.stack.imgur.com/XMGMs.png
As you can see the time serie do not cover all the years completely, so, when I have got no data ggplot links two subsequent years. I think I have to group the data in some ways, but I do not understand how. Here is the code:
df4 <- data.frame(df$Date, df$A)
colnames(df4)<- c("date","A")
df4$date <- as.Date(df4$date,"%Y/%m/%d")
df4$week_day <- as.numeric(format(df4$date, format='%w'))
df4$endofweek <- df4$date + (6 - df4$week_day)
week_aveA <- df4 %>%
group_by(endofweek) %>%
summarise_all(list(mean=mean), na.rm=TRUE) %>%
na.omit()
g1 = ggplot() +
geom_step(data=week_aveA, aes(group = 1, x = (endofweek), y = (A_mean)), colour="gray25") +
scale_y_continuous(expand = c(0, 0), limits = c(0, 2500)) +
scale_x_date(breaks="year", labels=date_format("%Y")) +
labs(y = expression(A~ ~index),
x = NULL) +
theme(axis.text.x = element_text(size=10),
axis.title = element_text(size=10))
Here an extraction (the former three years) of the dataset:
endofweek date_mean A_mean week_day_mean
1 20/03/2010 17/03/2010 939,2533437 3
2 27/03/2010 24/03/2010 867,3620121 3
3 03/04/2010 31/03/2010 1426,791222 3
4 10/04/2010 07/04/2010 358,5698314 3
5 17/04/2010 13/04/2010 301,1815352 2
6 24/04/2010 21/04/2010 273,4922895 3,333333333
7 01/05/2010 28/04/2010 128,5989633 3
8 08/05/2010 05/05/2010 447,8858881 3
9 15/05/2010 12/05/2010 387,9828891 3
10 22/05/2010 19/05/2010 138,0770986 3
11 29/05/2010 26/05/2010 370,2147933 3
12 05/06/2010 02/06/2010 139,0451791 3
13 12/06/2010 09/06/2010 217,1286356 3
14 19/06/2010 16/06/2010 72,36972411 3
15 26/06/2010 23/06/2010 282,2911902 3
16 03/07/2010 30/06/2010 324,3215936 3
17 10/07/2010 07/07/2010 210,568691 3
18 17/07/2010 14/07/2010 91,76930829 3
19 24/07/2010 21/07/2010 36,4211218 3,666666667
20 31/07/2010 28/07/2010 37,53981103 3
21 07/08/2010 04/08/2010 91,33282642 3
22 14/08/2010 11/08/2010 28,38587352 3
23 21/08/2010 18/08/2010 58,72836406 3
24 28/08/2010 24/08/2010 102,1050612 2,5
25 04/09/2010 02/09/2010 13,45357513 4,5
26 11/09/2010 08/09/2010 51,24017212 3
27 18/09/2010 15/09/2010 159,7395663 3
28 25/09/2010 21/09/2010 62,71136678 2
29 02/04/2011 31/03/2011 1484,661164 4
30 09/04/2011 06/04/2011 656,1827964 3
31 16/04/2011 13/04/2011 315,3097313 3
32 23/04/2011 20/04/2011 293,2904042 3
33 30/04/2011 26/04/2011 255,7517519 2,4
34 07/05/2011 04/05/2011 360,7035289 3
35 14/05/2011 11/05/2011 342,0902797 3
36 21/05/2011 18/05/2011 386,1380421 3
37 28/05/2011 24/05/2011 418,9624807 2,833333333
38 04/06/2011 01/06/2011 112,7568 3
39 11/06/2011 08/06/2011 85,17855619 3,2
40 18/06/2011 15/06/2011 351,8714638 3
41 25/06/2011 22/06/2011 139,7936898 3
42 02/07/2011 29/06/2011 68,57716191 3,6
43 09/07/2011 06/07/2011 62,31823822 3
44 16/07/2011 13/07/2011 80,7328917 3
45 23/07/2011 20/07/2011 114,9475331 3
46 30/07/2011 27/07/2011 90,13118758 3
47 06/08/2011 03/08/2011 43,29372258 3
48 13/08/2011 10/08/2011 49,39935204 3
49 20/08/2011 16/08/2011 133,746822 2
50 03/09/2011 31/08/2011 76,03928942 3
51 10/09/2011 05/09/2011 27,99834637 1
52 24/03/2012 23/03/2012 366,2625797 5,5
53 31/03/2012 28/03/2012 878,8535513 3
54 07/04/2012 04/04/2012 1029,909052 3
55 14/04/2012 11/04/2012 892,9163416 3
56 21/04/2012 18/04/2012 534,8278693 3
57 28/04/2012 25/04/2012 255,1177585 3
58 05/05/2012 02/05/2012 564,5280546 3
59 12/05/2012 09/05/2012 767,5018168 3
60 19/05/2012 16/05/2012 516,2680148 3
61 26/05/2012 23/05/2012 241,2113073 3
62 02/06/2012 30/05/2012 863,6123397 3
63 09/06/2012 06/06/2012 201,2019288 3
64 16/06/2012 13/06/2012 222,9955486 3
65 23/06/2012 20/06/2012 91,14166632 3
66 30/06/2012 27/06/2012 26,93145693 3
67 07/07/2012 04/07/2012 67,32183278 3
68 14/07/2012 11/07/2012 46,25297513 3
69 21/07/2012 18/07/2012 81,34359825 3,666666667
70 28/07/2012 25/07/2012 49,59130851 3
71 04/08/2012 01/08/2012 44,13438077 3
72 11/08/2012 08/08/2012 30,15773151 3
73 18/08/2012 15/08/2012 57,47256772 3
74 25/08/2012 22/08/2012 31,9109555 3
75 01/09/2012 29/08/2012 52,71058484 3
76 08/09/2012 04/09/2012 24,52495229 2
77 06/04/2013 01/04/2013 1344,388042 1,5
78 13/04/2013 10/04/2013 1304,838687 3
79 20/04/2013 17/04/2013 892,620141 3
80 27/04/2013 24/04/2013 400,1720434 3
81 04/05/2013 01/05/2013 424,8473083 3
82 11/05/2013 08/05/2013 269,2380208 3
83 18/05/2013 15/05/2013 238,9993749 3
84 25/05/2013 22/05/2013 128,4096151 3
85 01/06/2013 29/05/2013 158,5576121 3
86 08/06/2013 05/06/2013 175,2036942 3
87 15/06/2013 12/06/2013 79,20250839 3
88 22/06/2013 19/06/2013 126,9065428 3
89 29/06/2013 26/06/2013 133,7480108 3
90 06/07/2013 03/07/2013 218,0092943 3
91 13/07/2013 10/07/2013 54,08460936 3
92 20/07/2013 17/07/2013 91,54285041 3
93 27/07/2013 24/07/2013 44,64567928 3
94 03/08/2013 31/07/2013 229,5067999 3
95 10/08/2013 07/08/2013 49,70729373 3
96 17/08/2013 14/08/2013 53,38618335 3
97 24/08/2013 21/08/2013 217,2800997 3
98 31/08/2013 28/08/2013 49,43590136 3
99 07/09/2013 04/09/2013 64,88783029 3
100 14/09/2013 11/09/2013 11,04300773 3
So at the end I have one mainly question: how can I eliminated the connection between the years? ... and an aesthetic question: how can I add minor ticks on the x_axis? At least one every 6 months, just to make the plot easy to read.
Thanks in advance for any suggestion!
Edit
This is the code I tried with the suggestion, maybe I mistype some part of it.
library(tidyverse)
library(dplyr)
library(lubridate)
df4 <- data.frame(df$Date, df$A)
colnames(df4)<- c("date","A")
df4$date <- as.Date(df4$date,"%Y/%m/%d")
df4$week_day <- as.numeric(format(df4$date, format='%w'))
df4$endofweek <- df4$date + (6 - df4$week_day)
week_aveA <- df4 %>%
group_by(endofweek) %>%
summarise_all(list(mean=mean), na.rm=TRUE) %>%
na.omit()
week_aveA$endofweek <- as.Date(week_aveA$endofweek,"%d/%m/%Y")
week_aveA$A_mean <- as.numeric(gsub(",", ".", week_aveA$A_mean))
week_aveA$week_day_mean <- as.numeric(gsub(",", ".", week_aveA$week_day_mean))
week_aveA$year <- format(week_aveA$endofweek, "%Y")
library(ggplot2)
library(methods)
library(scales)
mylabel <- function(x) {
ifelse(grepl("-07-01$", x), "", format(x, "%Y"))
}
ggplot() +
geom_step(data=week_aveA, aes(x = endofweek, y = A_mean, group = year), colour="gray25") +
scale_y_continuous(expand = c(0, 0), limits = c(0, 2500)) +
scale_x_date(breaks="6 month", labels = mylabel) +
labs(y = expression(A~ ~index),
x = NULL) +
theme(axis.text.x = element_text(size=10),
axis.title = element_text(size=10))
You have to group by year:
Add a variable with the year to your dataset
Map the year variable on the group aesthetic
For the ticks. Increase the number of the breaks. If you want only ticks but not labels you can use a custom function to get rid of unwanted labels, e.g. my approach below set the breaks to "6 month" but replaces the mid-year labels with an empty string:
week_aveA$endofweek <- as.Date(week_aveA$endofweek,"%d/%m/%Y")
week_aveA$A_mean <- as.numeric(gsub(",", ".", week_aveA$A_mean))
week_aveA$week_day_mean <- as.numeric(gsub(",", ".", week_aveA$week_day_mean))
week_aveA$year <- format(week_aveA$endofweek, "%Y")
library(ggplot2)
mylabel <- function(x) {
ifelse(grepl("-07-01$", x), "", format(x, "%Y"))
}
ggplot() +
geom_step(data=week_aveA, aes(x = endofweek, y = A_mean, group = year), colour="gray25") +
scale_y_continuous(expand = c(0, 0), limits = c(0, 2500)) +
scale_x_date(breaks="6 month", labels = mylabel) +
labs(y = expression(A~ ~index),
x = NULL) +
theme(axis.text.x = element_text(size=10),
axis.title = element_text(size=10))

How can I solve error in ggplot, which does not want to change label/legend/title size?

I tried to change my label, title and legend size.
My code looks as follows:
ggplot(data = KOEFS, mapping = aes(x = DAY, y = DATA, color = COL, group = GROUP, linetype = LT, shape = LT)) +
theme_bw() +
theme(panel.border = element_blank(),
legend.position = c(0.95,0.05),
legend.justification = c("right", "bottom"),
legend.key = element_rect(fill = "transparent", colour = "transparent"),
axis.line = element_line(colour = "grey"),
axis.title = element_text(size = 15),
axis.text = element_text(size = 15),
title = element_text(size = 15)) +
labs(linetype = " ", color = " ", shape = " ", y = "Coefficients", x = "Days", title = "Change of Coefficients") +
scale_color_manual(values = c("black", "grey80")) +
scale_linetype_manual(values = c("solid", "solid", "solid", "solid")) +
geom_line() + geom_point(size = 2) +
scale_x_continuous(limits = c(0,30))
Every thing works fine until, I include axis.text, title, and axis.title with element.text(size = 15)
I get the following error message:
Error in grid.Call.graphics(C_setviewport, vp, TRUE) :
non-finite location and/or size for viewport
I saw some one had similar problem at: How can I solve non-finite location and/or size for viewport error?
but the solution provided and accepted does not work for me, as I already set factor levels before plotting.
Does somebody had similar problem, and a solution for it?
after reading the data below, I further run this lines of code:
KOEFS <- gather(data = KOEFS, "GROUP", "DATA", NTdown:VOLAup)
KOEFS$LT <- as.factor(rep(c("NumTrans","ShareVolume","CurrVolume","Range"), rep(102, 4)))
KOEFS$LT <- factor(KOEFS$LT, levels = c("NumTrans", "ShareVolume", "CurrVolume", "Range"))
KOEFS$COL <- as.factor(rep(c("Decreased","Increased","Decreased","Increased","Decreased","Increased","Decreased","Increased"), rep(51, 8)))
KOEFS$COL <- factor(KOEFS$COL, levels = c("Increased", "Decreased"))
NTdown NTup SVdown SVup CVdown CVup VOLAdown
1 0.14899191 0.08148003 0.15841280 0.0638288369 0.1020302760 0.10222146 0.116851318
2 0.13051269 0.06906892 0.14221577 0.0521486431 0.0810476305 0.09533784 0.065921156
3 0.11694863 0.05831254 0.13024738 0.0451474714 0.0664893707 0.09077826 0.040345412
4 0.10672363 0.05293483 0.12148711 0.0385805473 0.0557626433 0.08649484 0.024490826
5 0.10025098 0.04641818 0.11657800 0.0315967628 0.0495169773 0.08205766 0.013541618
6 0.09430301 0.04358022 0.11170043 0.0273400774 0.0432282882 0.08029605 0.003433474
7 0.08914748 0.03859364 0.10745635 0.0195358450 0.0375492691 0.07476321 -0.004680790
8 0.08552563 0.03646874 0.10480377 0.0188775808 0.0335212013 0.07624625 -0.010468664
9 0.08104235 0.03298945 0.09989953 0.0171805034 0.0276726873 0.07631037 -0.016291156
10 0.07781996 0.02885366 0.09609854 0.0137201041 0.0231783244 0.07440963 -0.018921712
11 0.07450735 0.02524488 0.09296921 0.0085703531 0.0195249830 0.07079881 -0.021992839
12 0.07101033 0.02327362 0.08924211 0.0060817375 0.0155666116 0.06985667 -0.025635913
13 0.06794973 0.02243860 0.08610496 0.0039647108 0.0120529138 0.06938331 -0.026730654
14 0.06530509 0.02179962 0.08357832 0.0028891552 0.0092279622 0.06977909 -0.032178305
15 0.06449438 0.01981794 0.08326299 0.0013067857 0.0084695899 0.06947027 -0.031905011
16 0.06496461 0.01883886 0.08364658 0.0011255086 0.0083090363 0.07052373 -0.032844871
17 0.06603638 0.02020186 0.08564288 0.0008801970 0.0097677186 0.07147915 -0.032976386
18 0.06513171 0.02126113 0.08539987 0.0008289405 0.0092061277 0.07277205 -0.033440267
19 0.06423981 0.02109540 0.08426217 -0.0010669175 0.0078061850 0.07241190 -0.034940167
20 0.06161477 0.02047414 0.08201759 -0.0022199721 0.0053892684 0.07282262 -0.037769731
21 0.06039974 0.02047247 0.08246071 -0.0029129677 0.0056432381 0.07363748 -0.038678841
22 0.05877017 0.02220594 0.08130549 -0.0020284510 0.0041951373 0.07599121 -0.040769355
23 0.05741982 0.02175369 0.08021732 -0.0025503349 0.0027926915 0.07668633 -0.040758564
24 0.05706360 0.02349892 0.07986195 -0.0010521510 0.0021704752 0.07939500 -0.040513944
25 0.05493456 0.02305855 0.07702993 -0.0026068535 -0.0008517291 0.07908119 -0.041880562
26 0.05388527 0.02381083 0.07586362 -0.0017309668 -0.0022191343 0.08120732 -0.042539505
27 0.05142774 0.02446681 0.07273625 -0.0016729749 -0.0055952802 0.08248979 -0.044998980
28 0.05061467 0.02512193 0.07273723 0.0003171961 -0.0059635760 0.08566663 -0.044241771
29 0.04851960 0.02534996 0.07110656 -0.0002278385 -0.0079490309 0.08633063 -0.045656993
30 0.04693144 0.02488648 0.07022198 -0.0010550992 -0.0090203148 0.08675680 -0.046596012
31 0.04579241 0.02567150 0.06909610 -0.0012111583 -0.0103045499 0.08792421 -0.048663873
32 0.04511828 0.02702644 0.06861984 -0.0004991153 -0.0108715265 0.08991500 -0.048500754
33 0.04404680 0.02620342 0.06745473 -0.0018116178 -0.0121999030 0.08980288 -0.049113268
34 0.04271634 0.02486164 0.06646081 -0.0049335565 -0.0133352751 0.08777721 -0.050571724
35 0.04257505 0.02511198 0.06652978 -0.0053467693 -0.0134128003 0.08838432 -0.050643493
36 0.04239646 0.02563825 0.06647117 -0.0052475911 -0.0136475477 0.08951276 -0.050490612
37 0.04249412 0.02676800 0.06714266 -0.0042781634 -0.0130135061 0.09157831 -0.050671154
38 0.04198787 0.02813158 0.06711555 -0.0036592608 -0.0129936343 0.09327154 -0.051863535
39 0.04190203 0.02811825 0.06714777 -0.0047614395 -0.0129498875 0.09329520 -0.052709535
40 0.04208824 0.02700932 0.06703829 -0.0075295995 -0.0131132999 0.09160795 -0.052627407
41 0.04247420 0.02592846 0.06814567 -0.0106101269 -0.0120764881 0.08959655 -0.052779310
42 0.04330288 0.02564733 0.06950863 -0.0108185922 -0.0107857566 0.09032201 -0.051574263
43 0.04231630 0.02614074 0.06899950 -0.0107066574 -0.0113407657 0.09136013 -0.052389933
44 0.04163394 0.02759957 0.06977687 -0.0113231903 -0.0105379041 0.09166803 -0.051792372
45 0.04229685 0.02784572 0.07022608 -0.0117842793 -0.0100144761 0.09211089 -0.051663520
46 0.04266142 0.02851634 0.07118170 -0.0122907058 -0.0090121611 0.09245767 -0.051718855
47 0.04125909 0.02882647 0.06993326 -0.0129900826 -0.0102314745 0.09252886 -0.053156557
48 0.04061323 0.02930907 0.06949844 -0.0133867472 -0.0106371703 0.09285065 -0.054770663
49 0.03967719 0.03098564 0.06853413 -0.0121399320 -0.0116194769 0.09463108 -0.055855444
50 0.04027910 0.03288249 0.06945463 -0.0103513818 -0.0107236133 0.09692312 -0.055243335
51 0.04075332 0.03436526 0.06984241 -0.0093368180 -0.0103067457 0.09842236 -0.055466941
VOLAup DAY
1 0.14458531 0
2 0.11975179 1
3 0.10051863 2
4 0.09127743 3
5 0.08255025 4
6 0.07959472 5
7 0.07420400 6
8 0.07207079 7
9 0.06909257 8
10 0.06361952 9
11 0.06173837 10
12 0.06021715 11
13 0.05892485 12
14 0.05713567 13
15 0.05446328 14
16 0.05331263 15
17 0.05311597 16
18 0.05395554 17
19 0.05402643 18
20 0.05453807 19
21 0.05412117 20
22 0.05446712 21
23 0.05373710 22
24 0.05493665 23
25 0.05387809 24
26 0.05575779 25
27 0.05743840 26
28 0.05877749 27
29 0.05860256 28
30 0.05826196 29
31 0.05766675 30
32 0.05829412 31
33 0.05680935 32
34 0.05508495 33
35 0.05594698 34
36 0.05595266 35
37 0.05732971 36
38 0.05832081 37
39 0.05735339 38
40 0.05766477 39
41 0.05802200 40
42 0.05898915 41
43 0.05957173 42
44 0.06055118 43
45 0.06015749 44
46 0.06005520 45
47 0.06070907 46
48 0.06078798 47
49 0.06142987 48
50 0.06233575 49
51 0.06338650 50
Updating package GGally solved it...

R hist vs geom_hist break points

I am using both geom_hist and histogram in R with the same breakpoints but I get different graphs. I did a quick search, does anyone know what the definition breaks are and why they would be a difference
These produce two different plots.
set.seed(25)
data <- data.frame(Mos=rnorm(500, mean = 25, sd = 8))
data$Mos<-round(data$Mos)
pAge <- ggplot(data, aes(x=Mos))
pAge + geom_histogram(breaks=seq(0, 50, by = 2))
hist(data$Mos,breaks=seq(0, 50, by = 2))
Thanks
To get the same histogram in ggplot2 you specify the breaks inside scale_x_continuous and binwidth inside geom_histogram.
Additionally, hist and histograms in ggplot2 use different defaults to create the intervals:
hist: right-closed (left open) intervals. Default: right = TRUE
stat_bin (ggplot2): left-closed (right open) intervals. Default: right = FALSE
**hist** **ggplot2**
freq1 Freq freq2 Freq
1 (0,2] 0 [0,2) 0
2 (2,4] 2 [2,4) 2
3 (4,6] 2 [4,6) 1
4 (6,8] 1 [6,8) 2
5 (8,10] 6 [8,10) 2
6 (10,12] 9 [10,12) 7
7 (12,14] 24 [12,14) 17
8 (14,16] 27 [14,16) 26
9 (16,18] 39 [16,18) 31
10 (18,20] 48 [18,20) 46
11 (20,22] 52 [20,22) 43
12 (22,24] 38 [22,24) 57
13 (24,26] 44 [24,26) 36
14 (26,28] 46 [26,28) 52
15 (28,30] 39 [28,30) 39
16 (30,32] 31 [30,32) 33
17 (32,34] 30 [32,34) 26
18 (34,36] 24 [34,36) 29
19 (36,38] 18 [36,38) 27
20 (38,40] 9 [38,40) 12
21 (40,42] 5 [40,42) 6
22 (42,44] 4 [42,44) 0
23 (44,46] 1 [44,46) 5
24 (46,48] 1 [46,48) 0
25 (48,50] 0 [48,50) 1
I included the argument right = FALSE so the histogram intervalss are left-closed (right open) as they are in ggplot2. I added the labels in both plots, so it is easier to check the intervals are the same.
ggplot(data, aes(x = Mos))+
geom_histogram(binwidth = 2, colour = "black", fill = "white")+
scale_x_continuous(breaks = seq(0, 50, by = 2))+
stat_bin(binwidth = 2, aes(label=..count..), vjust=-0.5, geom = "text")
hist(data$Mos,breaks=seq(0, 50, by = 2), labels =TRUE, right =FALSE)
To check the frequencies in each bin:
freq <- cut(data$Mos, breaks = seq(0, 50, by = 2), dig.lab = 4, right = FALSE)
as.data.frame(table(frecuencias))

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))

Re-ordering the legend items in ggplot2 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a table with following data
marks cut but xut
1 49 51 67
2 53 47 76
3 54 46 67
4 54 46 56
5 55 45 65
6 55 45 75
7 55 45 45
8 55 45 33
9 55 45 43
10 56 45 53
11 56 45 23
12 56 44 78
13 56 44 45
When I plot the graph I get get the legend as cut but xut , I want the legend to be as xut but and cut i.e. I want to re-order the legends and present them in a manner whichI need
below is the code which I have implemented
install.packages("plyr")
install.packages("ggplot2")
install.packages("reshape2")
library("plyr")
library("reshape2")
library("ggplot2")
data=read.csv("data.csv")
attach(data)
data$marks <- factor(data$marks, levels
= data$marks[order(data$cut)])
c.data=melt(data, id.var="marks")
n.data = ddply(c.data,.(marks), transform, pos = cumsum(value) - 0.5*value)
n.data <- transform(n.data,variable = factor(levels = c("xut", "but", "cut")))
plot = ggplot(d.data, aes(x = marks, y = value)) + geom_bar(stat = "identity",mapping = aes(x = value, fill = variable)) + scale_y_continuous( breaks=seq(0,100, by = 10))+geom_text(aes(label = value, y = pos), size = 3, face="bold", colour="white") + scale_fill_manual(values=c("455555","333333","335566")) + theme(axis.line = element_line(),axis.text.x=element_text(angle=60,hjust=1,colour="white"),axis.text.y=element_text(colour="white"),axis.title.x = element_blank(),axis.title.y = element_blank(),panel.background = element_blank(),axis.ticks=element_blank()) + labs(fill="")+coord_cartesian(ylim=c(0,100)) + theme(legend.position = "bottom", legend.direction = "horizontal")
Reorder the levels of the variable with the groups in the long or melted version of the data. For example, using your data
foo <- read.table(text="marks cut but xut
1 49 51 67
2 53 47 76
3 54 46 67
4 54 46 56
5 55 45 65
6 55 45 75
7 55 45 45
8 55 45 33
9 55 45 43
10 56 45 53
11 56 45 23
12 56 44 78
13 56 44 45", header = TRUE)
Melt it into a suitable format
require(reshape2)
require(ggplot2)
bar <- melt(foo, id = "marks")
> head(bar)
marks variable value
1 1 cut 49
2 2 cut 53
3 3 cut 54
4 4 cut 54
5 5 cut 55
6 6 cut 55
Then set the levels on the variable factor containing the group labels
bar <- transform(bar,
variable = factor(variable, levels = c("xut", "but", "cut")))
Then plot
ggplot(bar) + geom_bar(mapping = aes(x = value, fill = variable))
As you don't show any plotting code I'm guessing what your actual plot code looks like, but as the above shows, at least the ordering is what you want...

Resources