Geom_area order in ggplot - r

I have sample cohort data that is not color-coded or displayed in proper chronological order when plotted with ggplot(). The following code is used to generate the plot:
library(ggplot2)
blues <- colorRampPalette(c('lightblue', 'darkblue'))
p <- ggplot(cohort.chart, aes(x=month, y=users, group=cohort))
p + geom_area(aes(fill = cohort)) +
scale_fill_manual(values = blues(12)) +
ggtitle('Users by cohort') +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
As you can see in the legend, the months are not in chronological order, and that is reflected in the plot. I have tried changing both the cohort and the month to a yearmon object with as.yearmon() but this produces the error that continuous variables cannot be used. How does one solve this issue in ggplot() ?
Here is the data
> dput(cohort.chart)
structure(list(cohort = structure(c(11L, 10L, 3L, 5L, 4L, 8L,
1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L,
6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L,
11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L,
3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L,
8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L,
7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L,
12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L,
10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L,
4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L,
9L, 7L, 6L, 2L, 12L), .Label = c("Apr 2017", "Aug 2017", "Dec 2016",
"Feb 2017", "Jan 2017", "Jul 2017", "Jun 2017", "Mar 2017", "May 2017",
"Nov 2016", "Oct 2016", "Sep 2017"), class = "factor"), month = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L), .Label = c("Oct 2016", "Nov 2016",
"Dec 2016", "Jan 2017", "Feb 2017", "Mar 2017", "Apr 2017", "May 2017",
"Jun 2017", "Jul 2017", "Aug 2017", "Sep 2017"), class = "factor"),
users = c(795, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 672, 92,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 526, 48, 26, 0, 0, 0, 0, 0,
0, 0, 0, 0, 506, 37, 14, 26, 0, 0, 0, 0, 0, 0, 0, 0, 514,
46, 18, 19, 37, 0, 0, 0, 0, 0, 0, 0, 532, 47, 16, 18, 22,
57, 0, 0, 0, 0, 0, 0, 589, 55, 15, 20, 24, 39, 88, 0, 0,
0, 0, 0, 548, 53, 18, 21, 25, 39, 62, 172, 0, 0, 0, 0, 559,
53, 20, 20, 23, 36, 61, 139, 133, 0, 0, 0, 538, 58, 17, 18,
22, 39, 54, 130, 99, 109, 0, 0, 519, 45, 16, 19, 26, 39,
50, 125, 96, 86, 115, 0, 530, 54, 18, 20, 23, 37, 51, 129,
88, 75, 85, 126)), row.names = c(NA, -144L), .Names = c("cohort",
"month", "users"), class = "data.frame")
The plot should resemble the following:

Based on the data frame your provided, your month column is in the right factor level, but your cohort column is not, so change the factor level of your cohort column to be the same as your month column.
library(ggplot2)
blues <- colorRampPalette(c('lightblue', 'darkblue'))
# Change the factor level
cohort.chart$cohort <- factor(cohort.chart$cohort, levels = levels(cohort.chart$month))
p <- ggplot(cohort.chart, aes(x=month, y=users, group=cohort))
p + geom_area(aes(fill = cohort)) +
scale_fill_manual(values = blues(12)) +
ggtitle('Users by cohort') +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

When you created the dataframe, R (automatically) converted the strings into factors, creating a new factor for each new cohort date in alphabetical order.
cohort.chart$cohort
[1]... 12 Levels: Apr 2017 Aug 2017 Dec 2016 Feb 2017 Jan 2017 Jul 2017 Jun 2017 Mar 2017 May 2017 ... Sep 2017
You can use as.yearmon to fix this, you just need to convert back to a factor afterwards.
cohort.chart$cohort <- as.yearmon(cohort.chart$cohort)
cohort.chart$cohort <- as.factor(cohort.chart$cohort)

Related

How to add trace to plotly scatter plot

p <- plot_ly(data = bData, x = ~`Maturity Date`, y = ~YVal, type = 'scatter', mode='markers',
symbol = ~Sym, symbols = c('circle-open','x-open','diamond-open','square-open') ,
text = ~paste(bData$Security,bData$Crncy, bData$YTM, bData$DM,sep = "<br>") ,hoverinfo = 'text'
)
Above code produces this plot.
Now to this chart I want to add a trace with scatter plot with color depending on Currency column.
I tried this but it produces combination of two field as the legend.
Basically I want to classify the plot based on currency type but also add overlay or trace based on column SYM as the symbol.
p <- plot_ly(data = bData, x = ~`Maturity Date`, y = ~YVal, type = 'scatter', mode='markers',
symbol = ~Sym, symbols = c('circle-open','x-open','diamond-open','square-open') ,
text = ~paste(bData$Security,bData$Crncy, bData$YTM, bData$DM,sep = "<br>") ,hoverinfo = 'text'
) %>%
add_trace(x = ~`Maturity Date`, y = ~YVal , color=~Crncy)
data:
bData <- structure(list(Crncy = structure(c(9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 3L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 5L, 9L, 9L, 9L, 9L, 9L, 9L,
5L, 9L, 9L, 9L, 9L, 6L, 5L, 9L, 9L, 3L, 9L, 5L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 5L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 7L, 6L, 7L, 6L, 9L,
7L, 7L, 3L, 2L, 7L, 9L, 9L, 9L, 9L, 8L, 9L, 9L, 9L, 10L, 9L,
9L, 4L, 4L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 4L, 9L, 9L,
9L, 5L, 9L, 9L, 9L, 9L, 5L, 9L, 5L, 9L, 2L, 9L, 5L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 2L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 1L, 5L, 1L, 9L, 9L, 9L,
9L, 9L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 6L, 9L, 9L,
9L, 9L, 1L, 1L, 9L, 9L, 9L, 9L, 9L, 1L, 9L, 9L), .Label = c("AUD",
"CAD", "CHF", "COP", "EUR", "GBP", "JPY", "PEN", "USD", "ZAR"
), class = "factor"), `Maturity Date` = structure(c(20772, 19689,
18969, 18969, 20815, 20119, 20865, 20864, 20134, 20873, 20873,
20887, 20011, 20897, 20162, 19797, 20908, 20908, 20923, 19841,
19107, 19107, 20941, 20935, 20936, 20936, 20953, 20049, 19138,
19860, 21005, 21027, 19562, 19562, 21014, 19222, 21047, 19950,
19264, 19285, 19292, 19292, 19323, 19382, 19381, 20000, 19404,
20176, 19437, 19875, 19875, 19508, 20635, 19555, 19555, 20658,
19038, 19628, 18946, 19745, 19746, 19021, 19042, 19042, 20545,
20623, 19047, 19412, 19415, 20178, 20178, 19611, 19807, 20168,
20551, 20640, 20957, 20223, 19858, 19692, 19158, 20258, 19720,
20269, 20999, 20999, 20290, 20278, 20300, 20300, 21029, 19753,
20318, 20328, 20423, 20120, 20223, 20240, 19335, 20594, 19510,
19905, 20073, 20347, 20392, 18897, 20962, 20994, 21009, 21043,
19287, 19505, 18899, 19006, 19081, 19323, 19373, 19203, 19417,
19415, 19430, 19469, 19492, 19527, 19599, 20344, 19638, 19655,
19675, 19688, 20068, 19711, 19780, 19803, 19838, 19865, 19892,
19890, 19940, 19962, 20706, 20011, 18927, 20041, 18949, 20777,
20116, 20145, 19041, 20156, 20177, 20174, 20173, 20205, 20208,
20235, 20248, 20249, 19523, 20521, 20588, 20574, 20465, 20482,
19400, 20588, 21021, 20649, 20389, 20409, 19950, 19600, 19601,
20346, 19658, 20747, 19657, 19656, 19657, 20307, 20347, 19259,
20087, 20810, 20077, 19349, 20118, 20483, 20112, 20109, 19392,
19594, 20144, 21056, 19407, 20749, 20573, 19296, 19300, 19300,
19310, 20041, 19346, 20907, 19976, 20744, 20202, 19132, 19132,
19132), class = "Date"), Sym = structure(c(4L, 3L, 4L, 1L, 2L,
3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L,
3L, 4L, 3L, 4L, 3L, 4L, 1L, 4L, 3L, 2L, 1L, 4L, 1L, 2L, 1L, 2L,
1L, 2L, 3L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 3L, 4L, 3L, 2L,
1L, 4L, 1L, 4L, 1L, 2L, 1L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L,
3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L,
3L, 4L, 1L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L,
3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L,
3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L,
3L, 4L, 3L, 4L, 3L, 2L, 1L, 2L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L,
3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L,
3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L,
3L, 4L, 3L, 4L, 3L, 2L, 1L, 2L, 3L, 4L, 3L, 4L, 3L, 2L, 3L, 4L,
3L, 4L, 1L, 2L, 1L, 2L, 1L, 2L, 3L, 4L, 4L, 4L, 4L), .Label = c("Axe",
"Axe, Owned", "None", "Owned"), class = "factor"), YVal = c(20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,
69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126,
127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139,
140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152,
153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165,
166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178,
179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204,
205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217,
218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229)), class = "data.frame", row.names = c(NA,
-210L))
Maybe is this what you are looking for? (I have used split from plotly):
library(plotly)
#Code
plot_ly(data = bData, x = ~`Maturity Date`, y = ~YVal, type = 'scatter', mode='markers',
symbol = ~Sym, symbols = c('circle-open','x-open','diamond-open','square-open') ,
split = ~Crncy,
text = ~paste(bData$Security,bData$Crncy, bData$YTM, bData$DM,sep = "<br>") ,
hoverinfo = 'text')
Output:
Update: Here somo other options for OP:
#Option 1
plot_ly(data = bData, x = ~`Maturity Date`, y = ~YVal, type = 'scatter', mode='markers',
symbol = ~Sym, symbols = c('circle-open','x-open','diamond-open','square-open') ,
text = ~paste(bData$Security,bData$Crncy, bData$YTM, bData$DM,sep = "<br>") ,
hoverinfo = 'text',legendgroup = 'group1'
) %>%
add_trace(x = ~`Maturity Date`, y = ~YVal , symbol=~Crncy,legendgroup = 'group2')
Output:
Option 2:
#Option 2
plot_ly(bData, x = ~`Maturity Date`, y = ~YVal, type = 'scatter', mode='markers',
legendgroup = 'group1',color = ~Sym) %>%
add_trace(y = ~YVal, legendgroup = 'group2',type = 'scatter', mode='markers',
color=~Crncy)
Output:

Summarizing data.frame with the function from "Cookbook for R" outputs only NA / NaN (needed for error bars in ggplot)

I created a line-plot with ggplot. Now I would like to add the 95%-confidence intervall to the data points. On my search for a way to do that, I found the function "SummarySE" from the "Cookbook for R" and tried to apply it to my data.
The problem I have is, that all the new, calculated columns only contain NA and NaN with the Warning message: In qt(conf.interval/2 + 0.5, datac$N - 1) : NaNs produced.
head() of the output I'm getting:
My data:
structure(list(J01 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), .Label = c("Tetrazykline",
"Penicilline", "Andere Beta-Lactame", "Sulfonamide & Trimethoprim",
"Makrolide, Lincosamide & Streptogramine", "Aminoglykoside",
"Chinolone", "Andere Antibiotika"), class = "factor"), Monat = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L), .Label = c("Jan 2018", "Feb 2018", "Mär 2018",
"Apr 2018", "Mai 2018", "Jun 2018", "Jul 2018", "Aug 2018", "Sep 2018",
"Okt 2018", "Nov 2018", "Dez 2018"), class = "factor"), DDDs = c(262,
5729, 3305, 495, 588, 153, 944, 922, 85, 6386, 3157, 546, 753,
120, 421, 419, 102, 6758, 3582, 682, 853, 167, 884, 1061, 122,
5628, 3395, 564, 520, 161, 501, 728, 208, 4976, 3380, 508, 620,
205, 704, 1106, 147, 4420, 3521, 634, 611, 110, 728, 1212, 133,
5717, 3783, 708, 582, 230, 593, 842, 387, 6191, 3796, 605, 410,
62, 595, 875, 130, 5309, 4084, 2010, 395, 163, 632, 1202, 84,
5266, 3437, 617, 568, 141, 623, 948, 60, 5972, 3485, 718, 518,
132, 588, 1034, 182, 4924, 3360, 573, 405, 96, 319, 1337)), row.names = c(NA,
-96L), class = "data.frame")
Does anybody now what the problem is and how I can fix it?

How to generate borders in the ggplot

I have a data set that look like this. There are 5 plots that are spatialy distributed. I want to draw a distribution map that will show the variation of a variable over the experiment. I use geom_tile to do that.
ggplot(aes(x = x, y = y), data = check) + geom_tile(aes(fill = HJD_6))
Is there any way to generate the borders around each plot. I used manualy geom_vline and geom_hline but there are problems whan I have bigger design that is not as regular as in the example.
check <- structure(list(Yta = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L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 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20), y = c(31, 31, 31, 31, 31, 31,
31, 31, 31, 31, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33, 33,
33, 33, 33, 33, 33, 33, 33, 33, 34, 34, 34, 34, 34, 34, 34, 34,
34, 34, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 36, 36, 36, 36,
36, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39,
39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 31, 31,
31, 31, 31, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 34, 34, 34, 34,
34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 37,
37, 37, 37, 37, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 39, 39,
39, 39, 39, 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 40, 40,
40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 42, 42, 42, 42,
42, 42, 42, 42, 42, 42, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 45,
45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 47, 47,
47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 50, 50, 50, 50,
50, 50, 50, 50, 50, 50, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 43, 43, 43, 43, 43, 43,
43, 43, 43, 43, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 45,
45, 45, 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46,
46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49,
50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 51,
51, 51, 51, 51, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 53, 53,
53, 53, 53, 53, 53, 53, 53, 53, 54, 54, 54, 54, 54, 54, 54, 54,
54, 54, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 56, 56, 56, 56,
56, 56, 56, 56, 56, 56, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57,
58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 59, 59, 59, 59, 59, 59,
59, 59, 59, 59, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60), RAD = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,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8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), HJD_6 = c(136L, NA,
NA, 170L, 133L, 55L, NA, 105L, 120L, 130L, 85L, 95L, NA, NA,
185L, 200L, 85L, 153L, 82L, 80L, 80L, NA, 110L, 130L, 222L, 150L,
NA, 70L, 90L, 172L, NA, 177L, NA, 97L, 65L, 133L, 62L, 52L, 95L,
190L, 154L, 55L, NA, NA, 180L, 130L, 90L, NA, NA, NA, NA, NA,
NA, NA, 148L, NA, NA, NA, 244L, 158L, NA, 164L, NA, NA, 224L,
NA, NA, 139L, 140L, NA, NA, 155L, 135L, 76L, 80L, 130L, NA, NA,
145L, NA, 75L, NA, 105L, 70L, 95L, NA, 95L, 115L, 140L, NA, NA,
NA, 135L, NA, 75L, 98L, 132L, 100L, 105L, 112L, NA, NA, 125L,
105L, 87L, 79L, NA, NA, NA, 165L, NA, 110L, 110L, 133L, 75L,
52L, 117L, 70L, 155L, 130L, 180L, 187L, 110L, 90L, 60L, 120L,
195L, 90L, 100L, 88L, NA, 90L, NA, 112L, 130L, 155L, 152L, 130L,
73L, 122L, 142L, 130L, 150L, 108L, NA, 86L, 125L, 90L, 119L,
125L, 206L, 100L, 95L, 40L, 160L, 222L, NA, 100L, 112L, NA, NA,
NA, 105L, 150L, 185L, NA, NA, 163L, 135L, 115L, NA, 155L, 183L,
NA, 126L, 122L, 150L, 140L, 134L, 80L, 213L, 152L, 63L, 75L,
70L, NA, 115L, 98L, 106L, 130L, NA, 123L, NA, 114L, 65L, 144L,
115L, 60L, NA, 100L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 174L,
50L, 102L, 153L, NA, NA, 85L, 132L, 85L, NA, 72L, 177L, 115L,
141L, 157L, 77L, 70L, NA, 115L, 90L, NA, NA, NA, 40L, NA, 115L,
145L, 100L, 70L, 80L, 151L, 120L, NA, 55L, 200L, NA, 120L, 170L,
185L, NA, NA, NA, 120L, 60L, NA, NA, 95L, 172L, 60L, 155L, NA,
191L, 85L, NA, 65L, 115L, 115L, 175L, 30L, 66L, 195L, 161L, 132L,
NA, 80L, 75L, 115L, NA, NA, 115L, 95L, 151L, 140L, 114L, 140L,
165L, 124L, 168L, 90L, 50L, 160L, NA, 81L, 142L, 135L, 42L, 160L,
NA, 130L, 50L, 172L, 94L, 120L, NA, 140L, NA, 145L, 120L, NA,
NA, 170L, 187L, NA, 141L, 200L, 102L, NA, NA, 136L, NA, NA, 121L,
NA, 60L, 175L, 140L, 175L, 195L, NA, 216L, 77L, 231L, 175L, 210L,
180L, 175L, 260L, NA, 160L, 172L, NA, 135L, 122L, 193L, 115L,
175L, 60L, 85L, 202L, 164L, 159L, 95L, 169L, 190L, 80L, 80L,
120L, NA, 115L, 130L, 172L, 155L, 75L, 72L, 170L, NA, NA, 65L,
75L, NA, NA, 190L, NA, NA, NA, NA, NA, NA, NA, 75L, NA, NA, 90L,
NA, 190L, NA, NA, NA, NA, 52L, NA, NA, NA, 90L, NA, NA, NA, NA,
NA, NA, 93L, 130L, 109L, NA, NA, NA, 100L, NA, NA, NA, NA, NA,
NA, 150L, NA, 202L, 161L, NA, NA, 120L, 50L, NA, 164L, NA, NA,
120L, NA, 138L, NA, NA, 154L, 60L, 57L, 195L, 130L, 75L, NA,
NA, NA, 54L, 95L, 59L, 65L, 52L, 72L, NA, NA, NA, NA, NA, NA,
67L, NA, NA, NA, 168L, NA, NA, NA, 100L, 120L, NA, 195L, 40L,
NA, NA, NA, NA, NA)), row.names = c(NA, -500L), class = c("tbl_df",
"tbl", "data.frame"), .Names = c("Yta", "x", "y", "RAD", "PLANTA",
"HJD_6"))
One idea is to find the convex hull and plot the geom_polygon. Drawing on How to draw neat polygons around scatterplot regions in ggplot2:
library(plyr)
find_hull <- function(df) df[chull(df$x, df$y), ]
hulls <- ddply(check, "Yta", find_hull)
ggplot(aes(x = x, y = y), data = d) +
geom_tile(aes(fill = HJD_6), colour = "white") +
geom_polygon(data = hulls, aes(x = x, y = y, group = Yta),
colour = "red", alpha = 0)
You can modify hulls to make the border prettier, but this solution is quite specific to this example:
hulls <- ddply(hulls, "Yta", function(df) {
df$x <- df$x + ifelse(df$x < mean(df$x), -.5, .5)
df$y <- df$y + ifelse(df$y < mean(df$y), -.5, .5)
df
})

How can I order the months chronologically in ggplot2 short of writing the months out?

I am trying to plot count v/s month
ggplot(dat, aes(x=month, y=count,group=region)) +
geom_line(data=mcount[mcount$region == "West coast", ],colour="black",stat="identity", position="dodge")+
geom_point(data=mcount[mcount$region == "West coast", ],colour="black", size=2, shape=21, fill="white")+
theme_bw()+
theme(legend.key = element_rect(colour = "black")) +
guides(fill = guide_legend(override.aes = list(colour = NULL)))+
ggsave("test.png",width=6, height=4,dpi=300)
But I want to order the months chronologically from Jan to Dec. How can I do this short of writing all the months out?
dput
structure(list(region = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("West coast", "Arizona", "Front range", "Flash flood alley",
"Mississippi valley", "Appalachians"), class = "factor"), month = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 4L, 12L, 11L,
5L, 2L, 9L, 8L, 6L, 10L, 3L, 7L, 8L, 10L, 5L, 1L, 6L, 7L, 4L,
6L, 8L, 2L, 1L, 7L, 5L, 3L, 11L, 12L, 9L, 10L, 2L, 7L, 3L, 6L,
12L, 11L, 10L, 9L, 4L, 1L, 11L, 4L, 2L, 1L, 12L, 9L, 3L, 8L,
5L, 6L, 10L, 7L, 5L, 8L, 11L, 12L, 4L, 3L, 9L, 2L), .Label = c("Apr",
"Dec", "Oct", "Mar", "May", "Jul", "Sep", "Jun", "Nov", "Aug",
"Jan", "Feb"), class = "factor"), count = c(566, 545, 427, 751,
357, 399, 568, 433, 454, 347, 511, 251, 267, 207, 167, 142, 417,
109, 117, 373, 207, 130, 125, 145, 7, 14, 2, 2, 7, 3, 107, 74,
135, 48, 80, 53, 117, 125, 59, 53, 103, 30, 21, 18, 8, 22, 26,
37, 20, 5, 11, 1, 96, 29, 109, 8, 33, 53, 6, 1, 5, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0)), .Names = c("region", "month", "count"), row.names = c(NA,
-72L), class = c("data.table", "data.frame"))
Use the built-in month.name or month.abb variable to specify the levels of your factor in the correct order. In this case, you have abbreviations so month.abb is appropriate.
your_data$month = factor(your_data$month, levels = month.abb)
I think creating the factor in the correct order is the best way, but you can also just order the axis using the limits argument of the discrete scale (see ?discrete_scale for more info).
+ scale_x_discrete(limits = month.abb)
Locales
If you are in a non-English locale, you can construct your own month name constants with a little date formatting (basically stolen from Brian Ripley in this R-Help thread):
month.name.loc = format(ISOdate(2004, 1:12, 1), "%B")
month.abb.loc = format(ISOdate(2004, 1:12, 1), "%b")
If you want to use month names/abbreviations from a different locale than you're in, the withr package is useful.

HLM software output to R: Separate Fixed Effects for each Random Effect

I'm trying to match the output from a model specified in another software, HLM, in R, here: http://justpaste.it/q10n
The model I've tried so far (a random slope with a correlated intercept), isn't matching up for the fixed effects:
m1 <- lmer(formula = mathach ~ 1 + freered + (1 + hrs | school), data = dat)
Here's the R output for comparison:
summary(m1)
Linear mixed model fit by REML ['lmerMod']
Formula:
mathach ~ 1 + freered + (1 + hrs | school)
Data: dat
REML criterion at convergence: 180.2
Scaled residuals:
Min 1Q Median 3Q Max
-2.1706 -0.4274 -0.0611 0.3395 4.3201
Random effects:
Groups Name Variance Std.Dev. Corr
school (Intercept) 102.96318 10.1471
hrs 6.03046 2.4557 -1.00
Residual 0.09409 0.3067
Number of obs: 100, groups: school, 10
Fixed effects:
Estimate Std. Error t value
(Intercept) 68.99782 0.59137 116.67
freered 0.48765 0.03025 16.12
Correlation of Fixed Effects:
(Intr)
freered -0.895
and the dataset:
dat <- structure(list(school = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L), .Label = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10"), class = "factor"),
student = 1:100, hrs = c(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 0L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 0L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 0L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L), mathach = c(100, 96, 92, 88, 84, 80, 76, 72, 68,
65, 95, 92, 89, 86, 83, 80, 77, 74, 71, 69, 90, 88, 86, 84,
82, 80, 78, 76, 74, 71, 85, 84, 83, 82, 81, 80, 79, 78, 77,
77, 80, 79.5, 79, 78.5, 78, 77.5, 77, 76.5, 76, 76, 75, 75.5,
76, 76.5, 77, 77.5, 78, 78.5, 79, 80, 70, 71, 72, 73, 74,
75, 76, 77, 78, 80, 65, 67, 69, 71, 73, 75, 77, 79, 81, 82,
60, 63, 66, 69, 72, 75, 78, 81, 84, 86, 55, 59, 63, 67, 71,
75, 79, 83, 87, 93), freered = c(30L, 30L, 30L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 27L, 27L, 27L, 27L, 27L, 27L, 27L,
27L, 27L, 27L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L, 25L,
25L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L)), .Names = c("school",
"student", "hrs", "mathach", "freered"), row.names = c(NA, -100L
), class = "data.frame")
Any suggestions?
Here's a start: we get closer by (1) putting in an interaction between (1+hrs) and freered in the fixed effects; (2) centering the hrs and freered variables (as stated in the HLM log):
library(lme4)
library(broom) ## for tidy() (cosmetic: may (???) need Github devel version?)
dat2 <- transform(dat,hrs=scale(hrs,scale=FALSE),
freered=scale(freered,scale=FALSE))
m1 <- lmer(formula = mathach ~ (1 + hrs)*freered + (1 + hrs | school),
data = dat2)
## equivalent, explicit response formula:
## ~ 1 + freered + hrs + freered : hrs + (1 + hrs | school)
print(tidy(m1),digits=3)
term estimate std.error statistic group
1 (Intercept) 77.5400 0.2677 289.619 fixed
2 hrs 0.0218 0.1245 0.175 fixed
3 freered 0.3811 0.0307 12.399 fixed
4 hrs:freered -0.2791 0.0143 -19.520 fixed
5 sd_(Intercept).school 0.8411 NA NA school
6 sd_hrs.school 0.3923 NA NA school
7 cor_(Intercept).hrs.school 0.1793 NA NA school
8 sd_Observation.Residual 0.3067 NA NA Residual

Resources