R ggplot2 facet_wrap() with scales = "free", but each panel having fixed - r

I want to produce a figure that will have several scatterplots. Each scatterplot should have its own axis limits, but I want the aspect ratio of each plot to be 1 so that the 1:1 line goes through the plot at a 45 degree angle.
Here's an example of what I have going on:
met <- structure(list(datetime = structure(c(946756800, 946756800, 946756800,946756800, 946756800, 946756800, 946756800, 960465600, 960465600,960465600), class = c("POSIXct", "POSIXt"), tzone = "UTC"), Met_Parameter = c("1/MO l","albedo", "Bowen r", "conv mix h", "mech mix h", "MO l", "sens h flux","wind s", "wstar", "z0"), `Original Met` = c(0.0746268656716418,0.15, 0.8, -999, 24, 13.4, -0.7, 4, 1.2, 1), `Converted Met` = c(-0.0105263157894737,0.15, 0.8, 600, 367, -95, 21.6, 4, 1.2, 1)), row.names = c(1L,2L, 3L, 4L, 5L, 6L, 7L, 1054L, 1055L, 1056L), class = "data.frame")
pOUT <-
ggplot2::ggplot(data = met,
aes(x = `Original Met`,
y = `Converted Met`)) +
ggplot2::geom_point()+
ggplot2::geom_abline(intercept = 0,
slope = 1
) +
ggplot2::facet_wrap("Met_Parameter",
scales = "free") +
ggplot2::ggtitle("Comparison of Original Meteorology Versus Up Over Down Meterology") +
ggplot2::theme_bw()
What I am missing still is how to make the x and y axes fixed within the plot.
Thanks!

Related

How to change the y-axis scale in plot for a forecast object?

I have the following graph made with plot. I basically plotted the outcome of an arima model. The problem, as you can see, is the y-axis. I want to rescale it so that it shows values as integers and not in scientific notation. I already tried with ylim = c(a,b) but it didn't work.
This is the data to plot:
structure(list(method = "ARIMA(1,2,0)", model = structure(list(
coef = c(ar1 = 0.165440211592995), sigma2 = 314372.871343033,
var.coef = structure(0.0387588365491072, .Dim = c(1L, 1L), .Dimnames = list(
"ar1", "ar1")), mask = TRUE, loglik = -201.464633423226,
aic = 406.929266846451, arma = c(1L, 0L, 0L, 0L, 1L, 2L,
0L), residuals = structure(c(0.144002762945477, -0.257594259049227,
169.62992413163, -40.455716409227, 3.98528254071288, 325.669119576814,
-277.933508979317, 161.058607396831, 100.485413762468, 161.981734397248,
-21.1101185099251, 467.511038095663, 167.408540762885, 264.467148159716,
-870.459264535865, 1471.66097350626, 116.971877311758, -159.918791518434,
967.205782005673, -64.1682010133445, -372.385939678148, 352.062155538701,
632.526018003249, 1002.33521590517, 479.534164073812, 461.147699502253,
-1091.4663608196, -614.056109041783), .Tsp = c(1, 28, 1), class = "ts"),
call = arima(x = corona_total$Total_Cases, order = c(1, 2,
0)), series = "corona_total$Total_Cases", code = 0L,
n.cond = 0L, nobs = 26L, model = list(phi = 0.165440211592995,
theta = numeric(0), Delta = c(2, -1), Z = c(1, 2, -1),
a = c(-779, 59138, 53578), P = structure(c(-2.22044604925031e-16,
2.86887593857152e-17, -5.56124814802562e-17, 2.86887593857152e-17,
-3.31423141286073e-17, -1.61722928090181e-32, -5.56124814802562e-17,
-3.75958688714994e-17, -5.56124814802562e-17), .Dim = c(3L,
3L)), T = structure(c(0.165440211592995, 1, 0, 0, 2,
1, 0, -1, 0), .Dim = c(3L, 3L)), V = structure(c(1, 0,
0, 0, 0, 0, 0, 0, 0), .Dim = c(3L, 3L)), h = 0, Pn = structure(c(1,
-5.4830714621183e-18, 1.21812129054869e-17, -5.48307146211831e-18,
-3.31423141286073e-17, -1.84889274661175e-32, 1.21812129054869e-17,
-3.75958688714994e-17, -5.56124814802562e-17), .Dim = c(3L,
3L))), x = structure(c(322, 400, 650, 888, 1128, 1694,
2036, 2502, 3089, 3858, 4636, 5883, 7375, 9172, 10149, 12462,
15113, 17660, 21157, 24747, 27980, 31506, 35713, 41035, 47021,
53578, 59138, 63919), .Tsp = c(1, 28, 1), class = "ts")), class = "Arima"),
level = c(80, 95), mean = structure(c(68571.1220751691, 73201.9225591844,
77829.1955946478, 82455.8850482763, 87082.4779540027, 91709.0548868236,
96335.6291770837, 100962.203030158, 105588.776810904, 110215.350579684,
114841.924346485, 119468.498112958, 124095.071879377, 128721.645645786,
133348.219412195, 137974.793178603, 142601.366945011, 147227.940711419,
151854.514477827, 156481.088244235, 161107.662010643, 165734.235777051,
170360.80954346, 174987.383309868, 179613.957076276, 184240.530842684,
188867.104609092, 193493.6783755, 198120.252141908, 202746.825908316,
207373.399674724, 211999.973441132, 216626.54720754, 221253.120973948,
225879.694740356, 230506.268506765, 235132.842273173, 239759.416039581,
244385.989805989, 249012.563572397, 253639.137338805, 258265.711105213,
262892.284871621, 267518.858638029, 272145.432404437, 276772.006170845,
281398.579937253, 286025.153703662, 290651.72747007, 295278.301236478,
299904.875002886, 304531.448769294, 309158.022535702, 313784.59630211,
318411.170068518, 323037.743834926, 327664.317601334, 332290.891367742,
336917.46513415, 341544.038900558), .Tsp = c(29, 88, 1), class = "ts"),
lower = structure(c(67852.5693904542, 71488.0378850631, 74869.4056219101,
78042.7559156995, 81035.3037876344, 83865.5016552685, 86546.988586515,
89090.4113186268, 91504.3946218833, 93796.1160212266, 95971.6728237902,
98036.3298321095, 99994.6937502293, 101850.840164951, 103608.408563905,
105270.675078771, 106840.60926587, 108320.919172912, 109714.087632186,
111022.401864165, 112247.977899771, 113392.780933102, 114458.642437768,
115447.274680314, 116360.283118863, 117199.177067588, 117965.378927058,
118660.232219392, 119285.008620154, 119840.914142592, 120329.094601246,
120750.640459457, 121106.591147333, 121397.938922313, 121625.632332819,
121790.579335963, 121893.650112513, 121935.679615911, 121917.46988679,
121839.792160025, 121703.388787634, 121508.974997728, 121257.240507039,
120948.851002351, 120584.449504222, 120164.657624752, 119690.076729762,
119161.289014511, 118578.858501069, 117943.331964511, 117255.239794357,
116515.096796942, 115723.402943843, 114880.644070922, 113987.29253211,
113043.807811626, 112050.637097962, 111008.215822666, 109916.968166633,
108777.307536393, 67472.1905761175, 70580.7621429779, 73302.5874546909,
75706.5864702675, 77834.1231526988, 79713.3753855171, 81365.1952663977,
82805.8644073931, 84048.5730632326, 85104.2982790952, 85982.3650762524,
86690.8252744766, 87236.7242194817, 87626.2949833746, 87865.1036821527,
87958.1607268483, 87910.0076619409, 87724.7860890043, 87406.2931723477,
86958.0269138267, 86383.2235034666, 85684.8884462828, 84865.8227394482,
83928.6450686659, 82875.8107702521, 81709.6281410368, 80432.2725549711,
79045.7987518185, 77552.151591525, 75953.1755121868, 74250.6228859222,
72446.1614324952, 70541.3808230744, 68537.798584461, 66436.8653962784,
64239.9698590982, 61948.4427995644, 59563.561168772, 57086.5515820169,
54518.5935412548, 51860.8223759273, 49114.3319330342, 46280.1770432903,
43359.3757867774, 40352.9115785747, 37261.7350923526, 34086.7660377659,
30828.8948056289, 27488.983993257, 24067.8698209688, 20566.3634495346,
16985.2522073028, 13325.3007348137, 9587.25205390016, 5771.82856756041,
1879.73299626436, -2088.35074420535, -6131.75671876397, -10249.8361987147,
-14441.9569333302), .Dim = c(60L, 2L), .Dimnames = list(NULL,
c("80%", "95%")), .Tsp = c(29, 88, 1), class = c("mts",
"ts", "matrix")), upper = structure(c(69289.674759884, 74915.8072333057,
80788.9855673855, 86869.0141808532, 93129.6521203709, 99552.6081183786,
106124.269767652, 112833.994741689, 119673.158999925, 126634.585138142,
133712.175869179, 140900.666393806, 148195.450008524, 155592.451126622,
163088.030260485, 170678.911278435, 178362.124624152, 186134.962249926,
193994.941323469, 201939.774624305, 209967.346121516, 218075.690621001,
226262.976649151, 234527.491939421, 242867.631033688, 251281.88461778,
259768.830291125, 268327.124531608, 276955.495663662, 285652.73767404,
294417.704748202, 303249.306422807, 312146.503267748, 321108.303025584,
330133.757147894, 339221.957677567, 348372.034433833, 357583.15246325,
366854.509725187, 376185.334984769, 385574.885889976, 395022.447212698,
404527.329236203, 414088.866273707, 423706.415304653, 433379.354716939,
443107.083144745, 452889.018392812, 462724.59643907, 472613.270508444,
482554.510211415, 492547.800741645, 502592.64212756, 512688.548533298,
522835.047604926, 533031.679858227, 543277.998104707, 553573.566912819,
563917.962101668, 574310.770264724, 69670.0535742206, 75823.0829753909,
82355.8037346047, 89205.1836262851, 96330.8327553065, 103704.73438813,
111306.06308777, 119118.541652923, 127128.980558576, 135326.402880273,
143701.483616717, 152246.170951439, 160953.419539271, 169816.996308198,
178831.335142237, 187991.425630358, 197292.726228081, 206731.095333834,
216302.735783307, 226004.149574644, 235832.10051782, 245783.58310782,
255855.796347471, 266046.121551069, 276352.103382299, 286771.433544331,
297301.936663213, 307941.557999181, 318688.352692291, 329540.476304445,
340496.176463526, 351553.785449769, 362711.713592006, 373968.443363436,
385322.524084435, 396772.567154431, 408317.241746781, 419955.270910389,
431685.428029961, 443506.533603539, 455417.452301683, 467417.090277392,
479504.392699952, 491678.341489281, 503937.9532303, 516282.277249338,
528710.393836741, 541221.412601694, 553814.470946882, 566488.732651987,
579243.386556237, 592077.645331285, 604990.74433659, 617981.94055032,
631050.511569476, 644195.754673588, 657416.985946874, 670713.539454249,
684084.766467015, 697530.034734447), .Dim = c(60L, 2L), .Dimnames = list(
NULL, c("80%", "95%")), .Tsp = c(29, 88, 1), class = c("mts",
"ts", "matrix")), x = structure(c(322, 400, 650, 888, 1128,
1694, 2036, 2502, 3089, 3858, 4636, 5883, 7375, 9172, 10149,
12462, 15113, 17660, 21157, 24747, 27980, 31506, 35713, 41035,
47021, 53578, 59138, 63919), .Tsp = c(1, 28, 1), class = "ts"),
series = "corona_total$Total_Cases", fitted = structure(c(321.855997237055,
400.257594259049, 480.37007586837, 928.455716409227, 1124.01471745929,
1368.33088042319, 2313.93350897932, 2340.94139260317, 2988.51458623753,
3696.01826560275, 4657.11011850993, 5415.48896190434, 7207.59145923711,
8907.53285184028, 11019.4592645359, 10990.3390264937, 14996.0281226882,
17819.9187915184, 20189.7942179943, 24811.1682010133, 28352.3859396781,
31153.9378444613, 35080.4739819968, 40032.6647840948, 46541.4658359262,
53116.8523004977, 60229.4663608196, 64533.0561090418), .Tsp = c(1,
28, 1), class = "ts"), residuals = structure(c(0.144002762945477,
-0.257594259049227, 169.62992413163, -40.455716409227, 3.98528254071288,
325.669119576814, -277.933508979317, 161.058607396831, 100.485413762468,
161.981734397248, -21.1101185099251, 467.511038095663, 167.408540762885,
264.467148159716, -870.459264535865, 1471.66097350626, 116.971877311758,
-159.918791518434, 967.205782005673, -64.1682010133445, -372.385939678148,
352.062155538701, 632.526018003249, 1002.33521590517, 479.534164073812,
461.147699502253, -1091.4663608196, -614.056109041783), .Tsp = c(1,
28, 1), class = "ts")), class = "forecast")
This is the code I used to make the plot (ignore the dotted exponential curve):
plot(forecast, shaded = TRUE, shadecols=NULL, lambda = NULL, col = 1, fcol = 4, pi.col=1,
pi.lty=2, ylim = NULL, main = "Out-of-Sample Forecast", ylab = "Number of Cases",
xlab = "Days (since 23/03/2020)") + abline(v = 28:29, col= "#FF000033", lty=1, lwd=5)
Output:
Can anyone please help me with this?
I couldn't load your object in my R session, so I'm assuming your plot works like a regular one.
You have 2 options.
Either you set options(scipen = 10) (or some high value), which is a quick fix, but if you need some plots with scientific notation and others without on the same graphics window, this will not work.
You define the axis yourself, with the format you need.
You can use axTicks(2) to get the position of default ticks and then format the labels as you need.
I recommend option 2. Here's a quick example :
x <- seq(1,10, l = 100)
y <- x*1e5
par(mfrow = c(1,2))
plot(x, y, main = "custom axis", yaxt = "n")
ticks <- axTicks(2) # get axis ticks
axis(2, at = ticks, labels = formatC(ticks, format = 'd')) # make axis
plot(x, y, main = "default axis")
Outputs :
You can take a look at other potential options in the answers to this post

Add character variable (weekdays) to plot

I want to add on this plot the weekday as text on top of the bars.
The only function to add text in ggplot I found, is "annotate", which does not work the way I want.
It should look like this:
Plot with weekdays
geom_text gives me this
Geom_text
My code:
ggplot(data = filter(T2G2_dayav, site %in% c("S17S", "S17N"), !is.na(distance)),
mapping = aes(as.factor(x = date_days))) +
geom_col(mapping = aes(y = T2pn_av, fill = as.factor(distance)),
position = position_dodge(width = 0.9)) +
theme_bw() + ylab("Particle Number (#/cm³), day-av") + xlab("Date") +
scale_y_continuous(limits = c(0, 30000)) +
scale_fill_discrete(name = "T2, Distance from road (m)") +
scale_color_grey(name = "Reference intrument G2") +
ggtitle("Day-averaged Particle Number (PN) per distance")
the head of my data:
distance date_days site T2pn_av T2pn_avambient T2wdir_med weekday Date G2pn_av G2pn_min G2pn_max G2ws_av G2ws_min G2ws_max G2wdir_med
<int> <dttm> <chr> <dbl> <dbl> <dbl> <chr> <dttm> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 -10 2017-07-18 S17N 28814.83 16917.831 110 Di 2017-07-18 13655.29 4621 105100 0.6781284 0 3.6 51.0
2 -10 2017-07-19 S17N 24210.95 15565.951 100 Mi 2017-07-19 10627.73 2908 67250 1.3673618 0 5.5 70.0
3 -10 2017-07-24 S17N 16143.44 7907.442 80 Mo 2017-07-24 11686.54 3582 55080 0.8178753 0 4.8 95.5
4 -10 2017-07-29 S17N 11762.56 5574.563 270 Sa 2017-07-29 12180.73 5413 45490 1.0304985 0 5.7 265.0
5 -10 2017-07-30 S17N 12138.22 6360.225 290 So 2017-07-30 10404.75 6113 23860 1.2385791 0 6.6 274.0
6 -10 2017-07-31 S17N 13815.32 9008.320 270 Mo 2017-07-31 11849.89 4595 46270 0.8554044 0 4.4 230.0
dput(head(T2G2_dayav))
structure(list(distance = c(-10L, -10L, -10L, -10L, -10L, -10L
), date_days = structure(c(1500328800, 1500415200, 1500847200,
1501279200, 1501365600, 1501452000), class = c("POSIXct", "POSIXt"
), tzone = "Europe/Berlin"), site = c("S17N", "S17N", "S17N",
"S17N", "S17N", "S17N"), T2pn_av = c(28814.8306772908, 24210.9512670565,
16143.442364532, 11762.5630630631, 12138.2247114732, 13815.3198380567
), T2pn_avambient = c(16917.8306772908, 15565.9512670565, 7907.44236453202,
5574.56306306306, 6360.22471147318, 9008.31983805668), T2wdir_med = c(110,
100, 80, 270, 290, 270), weekday = c("Di", "Mi", "Mo", "Sa",
"So", "Mo"), Date = structure(c(1500328800, 1500415200, 1500847200,
1501279200, 1501365600, 1501452000), class = c("POSIXct", "POSIXt"
), tzone = "Europe/Berlin"), G2pn_av = c(13655.2885517401, 10627.7329973352,
11686.5429216867, 12180.7308516181, 10404.7472642001, 11849.8893070109
), G2pn_min = c(4621, 2908, 3582, 5413, 6113, 4595), G2pn_max = c(105100,
67250, 55080, 45490, 23860, 46270), G2ws_av = c(0.678128438241936,
1.36736183524505, 0.817875347544022, 1.0304984658137, 1.23857912107,
0.855404388351763), G2ws_min = c(0, 0, 0, 0, 0, 0), G2ws_max = c(3.6,
5.5, 4.8, 5.7, 6.6, 4.4), G2wdir_med = c(51, 70, 95.5, 265, 274,
230)), .Names = c("distance", "date_days", "site", "T2pn_av",
"T2pn_avambient", "T2wdir_med", "weekday", "Date", "G2pn_av",
"G2pn_min", "G2pn_max", "G2ws_av", "G2ws_min", "G2ws_max", "G2wdir_med"
), row.names = c(NA, -6L), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), vars = c("distance", "date_days"), drop = TRUE, indices = list(
0L, 1L, 2L, 3L, 4L, 5L), group_sizes = c(1L, 1L, 1L, 1L,
1L, 1L), biggest_group_size = 1L, labels = structure(list(distance = c(-10L,
-10L, -10L, -10L, -10L, -10L), date_days = structure(c(1500328800,
1500415200, 1500847200, 1501279200, 1501365600, 1501452000), class = c("POSIXct",
"POSIXt"), tzone = "Europe/Berlin")), row.names = c(NA, -6L), class = "data.frame", vars = c("distance",
"date_days"), drop = TRUE, .Names = c("distance", "date_days"
)))
The idea is that you add a text on top of every bar (that's why vjust = 0, but you could also do vjust = -.5 to allow more space or vjust = 1.5 to put it in the bars, which is nice as well). The rest within the geom_text ist basically the same as in geom_col. But in general, you could put commonly used aesthetics in the first occurency within ggplot(aes(...)), as you already did with the x-value.
ggplot(data = filter(T2G2_dayav, site %in% c("S17S", "S17N"), !is.na(distance)),
mapping = aes(as.factor(x = date_days))) +
geom_col(mapping = aes(y = T2pn_av, fill = as.factor(distance)),
position = position_dodge(width = 0.9)) +
geom_text(aes(label = weekday, y = T2pn_av), vjust = -.5, # add these
position = position_dodge(width = 0.9)) + # lines
theme_bw() + ylab("Particle Number (#/cm³), day-av") + xlab("Date") +
scale_y_continuous(limits = c(0, 30000)) +
scale_fill_discrete(name = "T2, Distance from road (m)") +
scale_color_grey(name = "Reference intrument G2") +
ggtitle("Day-averaged Particle Number (PN) per distance")
The following should solve your problem with too many labels. It takes the highest label and places it in the center of the bars of that x-value. Find a plot below with additional rows added to your data:
T2G2_dayav <- rbind(T2G2_dayav %>% ungroup(), T2G2_dayav %>% ungroup() %>% mutate(distance = 5)) # add more observations for testing
T2G2_dayav <- T2G2_dayav %>% mutate(T2pn_av = ifelse(distance == 5, T2pn_av/2, T2pn_av)) # label only the highest bar
The following should work with your data:
ggplot(data = filter(T2G2_dayav, site %in% c("S17S", "S17N"), !is.na(distance)) %>%
group_by(date_days) %>% # group by days
mutate(weekday2 = ifelse(T2pn_av == max(T2pn_av), weekday, NA)), # within each day (group), only label the highest
mapping = aes(as.factor(x = date_days))) +
geom_col(mapping = aes(y = T2pn_av, fill = as.factor(distance)),
position = position_dodge(width = 0.9)) +
geom_text(aes(label = weekday2, y = T2pn_av), vjust = -.5, # add these
position = position_dodge(with = 0.9)) + # lines
theme_bw() + ylab("Particle Number (#/cm³), day-av") + xlab("Date") +
scale_y_continuous(limits = c(0, 30000)) +
scale_fill_discrete(name = "T2, Distance from road (m)") +
scale_color_grey(name = "Reference intrument G2") +
ggtitle("Day-averaged Particle Number (PN) per distance")

ggplot2: change the color of error bars w/o losing customized dodge parameters

I would like to change the color of my error bars to different colors without changing the position of the points on my graph.
Here is a bit of my data:
df <- structure(
list(
yrmonth = structure(
c(
1483228800,
1483228800,
1483228800,
1485907200,
1485907200,
1485907200,
1488326400,
1488326400,
1488326400,
1491004800,
1491004800,
1491004800
),
class = c("POSIXct", "POSIXt"),
tzone = "UTC"
),
index = structure(
c(1L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 5L),
.Label = c("N-S", "N-S", "E-W", "E-W", "OS"),
class = "factor"
),
N = c(2, 2, 1, 2, 2, 1, 2, 2, 1, 2, 2, 1),
GDDLettuce = c(129, 141, 27, 150.5, 209, 87, 247.5,
243, 188, 223, 226.5, 170),
sd = c(
1.4142135623731,
4.24264068711928,
NA,
4.94974746830583,
65.0538238691624,
NA,
12.0208152801713,
8.48528137423857,
NA,
5.65685424949238,
0.707106781186548,
NA
),
se = c(1, 3, NA, 3.5, 46, NA, 8.5, 6, NA, 4, 0.5, NA),
ci = c(
12.7062047361747,
38.1186142085241,
NA,
44.4717165766114,
584.485417864036,
NA,
108.002740257485,
76.2372284170481,
NA,
50.8248189446988,
6.35310236808735,
NA
)
),
.Names = c("yrmonth", "index", "N", "data", "sd", "se", "ci"),
row.names = 31:42,
class = "data.frame"
)
I have my graph set up exactly the way I want it with error bars in the right locations:
ggplot(df, aes(x=yrmonth,y=data,colour=factor(index))) +
geom_line(size=1, position = position_dodge(width = -300000)) + ylim(min(df$data), max(df$data)) +
geom_errorbar(aes(ymin=data-se, ymax=data+se), width = 1000000, size = .5,
position = position_dodge(width = -300000))
When I add one color, the color of the bars change, but it removes all the width and dodge parameters that I put in:
ggplot(df, aes(x=yrmonth,y=data,colour=factor(index))) +
geom_line(size=1, position = position_dodge(width = -300000)) + ylim(min(df$data), max(df$data)) +
geom_errorbar(aes(ymin=data-se, ymax=data+se), colour = "black", width = 1000000, size = .5,
position = position_dodge(width = -300000))
I would like the E-W error bars to be a dark green and the N-S error bars to be a dark red. I would like the lines themselves to stay the same color. Most importantly I want the error bars to stay in there same locations. For some reason, every time I change the color the position of my error bars and lines changes.
A quick and easy solution would be
require(dplyr)
ggplot(df %>% mutate(errorColors = ifelse(index=="N-S","darkred",ifelse(index=="E-W","darkgreen",NA))), aes(x=yrmonth,y=data)) +
geom_line(aes(group=index,color=index),size=1, position = position_dodge(width = -300000)) + ylim(min(df$data), max(df$data)) +
geom_errorbar(aes(ymin=data-se, ymax=data+se,color=errorColors), width = 1000000, size = .5,
position = position_dodge(width = -300000))+scale_color_manual(breaks=c("E-W","N-S","OS"),values=c("darkgreen","darkred","green","red","blue"))
try add one more layer scale_color_manual in your first plot code like this:
ggplot(df, aes(x=yrmonth,y=data,colour=factor(index))) +
geom_line(size=1, position = position_dodge(width = -300000)) + ylim(min(df$data), max(df$data)) +
geom_errorbar(aes(ymin=data-se, ymax=data+se), width = 1000000, size = .5,
position = position_dodge(width = -300000))+
scale_color_manual(values = c('darkblue', 'darkgreen', 'darkred'))

Putting x-axis labels directly under tick marks in barplots in R

I have a table (below) showing the percentage of tree species (categorical variable) present in a group experiment. My objective is to plot the percentage of tree species on the y-axis and 'Species' on the x-axis within a barplot.
Issue
My problem is that I am experiencing problems with formatting the x-axis correctly. My objective is to ensure that the x-axis labels for**'Species'** are:-
Positioned directly underneath their bar at the tick mark
Do not overlap onto the plotting area
If anyone can help solve this issue, I would be incredibly grateful.
R code
df <- leaf.percent[order(leaf.percent$Leaf.Percentge, decreasing = TRUE),]
Tree.labels<-c("Quercus robar", "Quercus Patraea",
"Deciduous", "Oak",
"Plant", "Shrub")
par(mar=c(6, 6, 3, 3))
Tree<-barplot(df$Leaf.Percentge, names.arg = df$Species,
xaxt = "n",
ylab="Percentage %",
xlab="Tree Species",
col="lightblue",
ylim = c(0, 60))
axis(1, at=Tree, labels=FALSE)
text(seq(1, 6, by=1), par("usr")[3] - 0.2,
labels=unique(Tree.labels),
srt = 25, pos = 1,
xpd = TRUE, cex=0.7)
DATA
structure(list(Species = structure(1:6, .Label = c("Deciduous",
"Oak", "Plant", "Quercus_petraea", "Quercus_robur", "Shrub"), class = "factor"),
Frequency = c(48L, 29L, 6L, 70L, 206L, 4L), Leaf.Percentge = c(13.2231404958678,
7.98898071625344, 1.65289256198347, 19.2837465564738, 56.7493112947658,
1.10192837465565)), .Names = c("Species", "Frequency", "Leaf.Percentge"
), row.names = c(NA, -6L), class = "data.frame")

How to change the color of dendrogram for each group in a cluster

Here is my data
df<- structure(list(name = structure(c(2L, 12L, 1L, 16L, 14L, 10L,
9L, 5L, 15L, 4L, 8L, 13L, 7L, 6L, 3L, 11L), .Label = c("All",
"Bab", "boro", "bra", "charli", "delta", "few", "hora", "Howe",
"ist", "kind", "Kiss", "myr", "No", "TT", "where"), class = "factor"),
value = c(1.251, -1.018, -1.074, -1.137, 1.018, 1.293, 1.022,
-1.008, 1.022, 1.252, -1.005, 1.694, -1.068, 1.396, 1.646,
1.016)), .Names = c("name", "value"), class = "data.frame", row.names = c(NA,
-16L))
here what I do
d <- dist(as.matrix(df$value),method = "euclidean")
#compute cluster membership
hcn <- hclust(d,method = "ward.D2")
plot(hcn)
and it gives me what I want as follows
Here all groups are shown by black color and the dendrogram is not that clear what I want is to change the color of each group and also use the name in vertical instead the number and finally I want to be able to remo the hclust(."ward.D2") while change the x label and y label as I want
You could use the dendextend package, aimed for tasks such as this:
# install the package:
if (!require('dendextend')) install.packages('dendextend'); library('dendextend')
## Example:
dend <- as.dendrogram(hclust(dist(USArrests), "ave"))
d1=color_branches(dend,k=5, col = c(3,1,1,4,1))
plot(d1) # selective coloring of branches :)
d2=color_branches(d1,k=5) # auto-coloring 5 clusters of branches.
plot(d2)
# More examples are in ?color_branches
You can see many examples in the presentations and vignettes of the package, in the "usage" section in the following URL: https://github.com/talgalili/dendextend
Or you can use also:
You should use dendrapply.
For instance:
# Generate data
set.seed(12345)
desc.1 <- c(rnorm(10, 0, 1), rnorm(20, 10, 4))
desc.2 <- c(rnorm(5, 20, .5), rnorm(5, 5, 1.5), rnorm(20, 10, 2))
desc.3 <- c(rnorm(10, 3, .1), rnorm(15, 6, .2), rnorm(5, 5, .3))
data <- cbind(desc.1, desc.2, desc.3)
# Create dendrogram
d <- dist(data)
hc <- as.dendrogram(hclust(d))
# Function to color branches
colbranches <- function(n, col)
{
a <- attributes(n) # Find the attributes of current node
# Color edges with requested color
attr(n, "edgePar") <- c(a$edgePar, list(col=col, lwd=2))
n # Don't forget to return the node!
}
# Color the first sub-branch of the first branch in red,
# the second sub-branch in orange and the second branch in blue
hc[[1]][[1]] = dendrapply(hc[[1]][[1]], colbranches, "red")
hc[[1]][[2]] = dendrapply(hc[[1]][[2]], colbranches, "orange")
hc[[2]] = dendrapply(hc[[2]], colbranches, "blue")
# Plot
plot(hc)
I get this information from: How to create a dendrogram with colored branches?
We could instead draw rectangles around groups, let's say there are 5 groups(k = 5):
# plot dendogram
plot(hcn)
# then draw dendogram with red borders around the 5 clusters
rect.hclust(hcn, k = 5, border = "red")
EDIT:
Remove x axis label, and add names instead of numbers:
plot(hcn, xlab = NA, sub = NA, labels = df$name)
rect.hclust(hcn, k = 5, border = "red")

Resources