Drawing SE in xyplot with errorbars - r

I am trying to construct a simple XY-Graph with the milk production (called FCM) of two different groups of cows (from the output I got from the mixed model, using the lsmeans and SE).
I was able to construct the plot displaying the lsmeans using the xyplot function in lattice:
library(lattice)
xyplot(lsmean~Time, type="b", group=Group, data=lsmeans2[order(lsmeans2$Time),],
pch=16, ylim=c(10,35), col=c("darkorange","darkgreen"),
ylab="FCM (kg/day)", xlab="Week", lwd=2,
key=list(space="top",
lines=list(col=c("darkorange","darkgreen"),lty=c(1,1),lwd=2),
text=list(c("Confinement Group","Pasture Group"), cex=0.8)))
I now want to add the error bars. I tried some things with the panel.arrow function, just copying and pasting from other examples but didn´t get any further.
I would really appreciate some help!
My lsmeans2 dataset:
Group Time lsmean SE df lower.CL upper.CL
Stall wk1 26.23299 0.6460481 59 24.19243 28.27356
Weide wk1 25.12652 0.6701080 58 23.00834 27.24471
Stall wk10 21.89950 0.6460589 59 19.85890 23.94010
Weide wk10 18.45845 0.6679617 58 16.34705 20.56986
Stall wk2 25.38004 0.6460168 59 23.33957 27.42050
Weide wk2 22.90409 0.6679617 58 20.79269 25.01549
Stall wk3 25.02474 0.6459262 59 22.98455 27.06492
Weide wk3 24.05886 0.6679436 58 21.94751 26.17020
Stall wk4 23.91630 0.6456643 59 21.87694 25.95565
Weide wk4 22.23608 0.6678912 58 20.12490 24.34726
Stall wk5 23.97382 0.6493483 59 21.92283 26.02481
Weide wk5 18.14550 0.6677398 58 16.03480 20.25620
Stall wk6 24.48899 0.6456643 59 22.44963 26.52834
Weide wk6 19.40022 0.6697394 58 17.28319 21.51724
Stall wk7 24.98107 0.6459262 59 22.94089 27.02126
Weide wk7 19.71200 0.6677398 58 17.60129 21.82270
Stall wk8 22.65167 0.6460168 59 20.61120 24.69214
Weide wk8 19.35759 0.6678912 58 17.24641 21.46877
Stall wk9 22.64381 0.6460481 59 20.60324 24.68438
Weide wk9 19.26869 0.6679436 58 17.15735 21.38004

For completeness, here is a solution using xyplot:
# Reproducible data
lsmeans2 = structure(list(Group = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("Stall",
"Weide"), class = "factor"), Time = structure(c(1L, 1L, 2L, 2L,
3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L,
10L), .Label = c("wk1", "wk10", "wk2", "wk3", "wk4", "wk5", "wk6",
"wk7", "wk8", "wk9"), class = "factor"), lsmean = c(26.23299,
25.12652, 21.8995, 18.45845, 25.38004, 22.90409, 25.02474, 24.05886,
23.9163, 22.23608, 23.97382, 18.1455, 24.48899, 19.40022, 24.98107,
19.712, 22.65167, 19.35759, 22.64381, 19.26869), SE = c(0.6460481,
0.670108, 0.6460589, 0.6679617, 0.6460168, 0.6679617, 0.6459262,
0.6679436, 0.6456643, 0.6678912, 0.6493483, 0.6677398, 0.6456643,
0.6697394, 0.6459262, 0.6677398, 0.6460168, 0.6678912, 0.6460481,
0.6679436), df = c(59L, 58L, 59L, 58L, 59L, 58L, 59L, 58L, 59L,
58L, 59L, 58L, 59L, 58L, 59L, 58L, 59L, 58L, 59L, 58L), lower.CL = c(24.19243,
23.00834, 19.8589, 16.34705, 23.33957, 20.79269, 22.98455, 21.94751,
21.87694, 20.1249, 21.92283, 16.0348, 22.44963, 17.28319, 22.94089,
17.60129, 20.6112, 17.24641, 20.60324, 17.15735), upper.CL = c(28.27356,
27.24471, 23.9401, 20.56986, 27.4205, 25.01549, 27.06492, 26.1702,
25.95565, 24.34726, 26.02481, 20.2562, 26.52834, 21.51724, 27.02126,
21.8227, 24.69214, 21.46877, 24.68438, 21.38004)), .Names = c("Group",
"Time", "lsmean", "SE", "df", "lower.CL", "upper.CL"), class = "data.frame", row.names = c(NA,
-20L))
xyplot(lsmean~Time, type="b", group=Group, data=lsmeans2[order(lsmeans2$Time),],
panel = function(x, y, ...){
panel.arrows(x, y, x, lsmeans2$upper.CL, length = 0.15,
angle = 90, col=c("darkorange","darkgreen"))
panel.arrows(x, y, x, lsmeans2$lower.CL, length = 0.15,
angle = 90, col=c("darkorange","darkgreen"))
panel.xyplot(x,y, ...)
},
pch=16, ylim=c(10,35), col=c("darkorange","darkgreen"),
ylab="FCM (kg/day)", xlab="Week", lwd=2,
key=list(space="top",
lines=list(col=c("darkorange","darkgreen"),lty=c(1,1),lwd=2),
text=list(c("Confinement Group","Pasture Group"), cex=0.8)))
The length argument in panel.arrows changes the width of the error heads. You can fiddle around with this parameter to get a width you like.
Notice that even though you had lsmeans2[order(lsmeans2$Time),] when specifying the data =, the ordering of Time is still wrong. This is because Time is a factor, and R doesn't know you want it to order by the numerical suffix of wk. This means, that it will sort wk10 before wk2, because 1 is smaller than 2. You can use this little trick below to order it correctly:
# Order first by the character lenght, then by Time
Timelevels = levels(lsmeans2$Time)
Timelevels = Timelevels[order(nchar(Timelevels), Timelevels)]
# Reorder the levels
lsmeans2$Time = factor(lsmeans2$Time, levels = Timelevels)
# Create Subset
lsmeansSub = lsmeans2[order(lsmeans2$Time),]
xyplot(lsmean~Time, type="b", group=Group, data=lsmeansSub,
panel = function(x, y, yu, yl, ...){
panel.arrows(x, y, x, lsmeansSub$upper.CL, length = 0.15,
angle = 90, col=c("darkorange","darkgreen"))
panel.arrows(x, y, x, lsmeansSub$lower.CL, length = 0.15,
angle = 90, col=c("darkorange","darkgreen"))
panel.xyplot(x, y, ...)
},
pch=16, ylim=c(10,35), col=c("darkorange","darkgreen"),
ylab="FCM (kg/day)", xlab="Week", lwd=2,
key=list(space="top",
lines=list(col=c("darkorange","darkgreen"),lty=c(1,1),lwd=2),
text=list(c("Confinement Group","Pasture Group"), cex=0.8)))
Note that even after reordering the the levels of "Time", I still need to use the sorted data for the data = argument. This is because xyplot plots the points in the order that appears in the dataset, not the order of the factor levels.

Is there a particular reason you want to use xplot? ggplot2 is much easier to work with and prettier. Here's an example of what I think you want.
#load ggplot2
library(ggplot2)
#load data
d = structure(list(Group = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("Stall",
"Weide"), class = "factor"), Time = structure(c(1L, 1L, 2L, 2L,
3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L,
10L), .Label = c("wk1", "wk10", "wk2", "wk3", "wk4", "wk5", "wk6",
"wk7", "wk8", "wk9"), class = "factor"), lsmean = c(26.23299,
25.12652, 21.8995, 18.45845, 25.38004, 22.90409, 25.02474, 24.05886,
23.9163, 22.23608, 23.97382, 18.1455, 24.48899, 19.40022, 24.98107,
19.712, 22.65167, 19.35759, 22.64381, 19.26869), SE = c(0.6460481,
0.670108, 0.6460589, 0.6679617, 0.6460168, 0.6679617, 0.6459262,
0.6679436, 0.6456643, 0.6678912, 0.6493483, 0.6677398, 0.6456643,
0.6697394, 0.6459262, 0.6677398, 0.6460168, 0.6678912, 0.6460481,
0.6679436), df = c(59L, 58L, 59L, 58L, 59L, 58L, 59L, 58L, 59L,
58L, 59L, 58L, 59L, 58L, 59L, 58L, 59L, 58L, 59L, 58L), lower.CL = c(24.19243,
23.00834, 19.8589, 16.34705, 23.33957, 20.79269, 22.98455, 21.94751,
21.87694, 20.1249, 21.92283, 16.0348, 22.44963, 17.28319, 22.94089,
17.60129, 20.6112, 17.24641, 20.60324, 17.15735), upper.CL = c(28.27356,
27.24471, 23.9401, 20.56986, 27.4205, 25.01549, 27.06492, 26.1702,
25.95565, 24.34726, 26.02481, 20.2562, 26.52834, 21.51724, 27.02126,
21.8227, 24.69214, 21.46877, 24.68438, 21.38004)), .Names = c("Group",
"Time", "lsmean", "SE", "df", "lower.CL", "upper.CL"), class = "data.frame", row.names = c(NA,
-20L))
#fix week
library(stringr)
library(magrittr)
d$Time %<>% as.character() %>% str_replace(pattern = "wk", replacement = "") %>% as.numeric()
#plot
ggplot(d, aes(Time, lsmean, color = Group, group = Group)) +
geom_point() +
geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL), width = .2) +
geom_line() +
ylim(10, 35) +
scale_x_continuous(name = "Week", breaks = 1:10) +
ylab("FCM (kg/day)") +
scale_color_discrete(label = c("Confinement Group","Pasture Group"))

Related

How to make scatterplot with colors based on a column and add a mean line through stats_summary with grouping based on another column?

I have a data.frame (see below) and I would like to build a scatterplot, where colours of dots is based on a factor column (replicate). I simultaneously want to add a line that represents the mean of y, for each x. The problem is that when I define the stat_summary it uses the colours I requested for groupingand hence I get three mean lines (for each color) instead of one. Trying to redefine groups either in ggplot() or stat_summary() function did not work.
if I disable colors I get what I want (a single mean line).
How do I have colors (plot # 1), yet still have a single mean line (plot # 2)?
structure(list(conc = c(10L, 10L, 10L, 25L, 25L, 25L, 50L, 50L,
50L, 75L, 75L, 75L, 100L, 100L, 100L, 200L, 200L, 200L, 300L,
300L, 300L, 400L, 400L, 400L, 500L, 500L, 500L, 750L, 750L, 750L,
1000L, 1000L, 1000L), citric_acid = c(484009.63, 409245.09, 303193.26,
426427.47, 332657.35, 330875.96, 447093.71, 344837.39, 302873.98,
435321.69, 359146.09, 341760.28, 378298.37, 342970.87, 323146.92,
362396.98, 361246.41, 290638.14, 417357.82, 351927.66, 323611.37,
416280.3, 359430.65, 327950.99, 431167.14, 361429.91, 291901.43,
340166.41, 353640.91, 341839.08, 393392.69, 311375.19, 342103.54
), MICIT = c(20771.28, 18041.97, 12924.35, 49814.13, 38683.32,
38384.72, 106812.16, 82143.12, 72342.43, 156535.39, 128672.12,
119397.14, 187208.46, 167814.92, 159418.62, 350813.47, 357227.48,
295948.31, 505553.77, 523282.46, 489652.3, 803544.84, 704431.61,
654753.29, 1030485.41, 895451.64, 717698.52, 1246839.19, 1309712.63,
1212111.53, 1930503.38, 1499838.89, 1642091.64), replicate = structure(c(1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L
), .Label = c("1", "2", "3"), class = "factor"), MICITNorm = c(0.0429150139016862,
0.0440859779160698, 0.0426274317575529, 0.116817357005636, 0.116285781751102,
0.116009395182412, 0.238903293897827, 0.238208275500519, 0.238853235263062,
0.359585551549246, 0.358272367659634, 0.34935932285636, 0.494869856298879,
0.489297881187402, 0.493331701877276, 0.968036405822146, 0.98887482369721,
1.01827072661558, 1.21131974956166, 1.48690347328766, 1.51308744189056,
1.93029754230503, 1.95985403582026, 1.99649737297637, 2.38999059622215,
2.47752500616233, 2.45870162403795, 3.6653801002868, 3.70350995307641,
3.54585417793659, 4.90731889298706, 4.81682207885606, 4.79998435561351
)), class = "data.frame", row.names = c(NA, -33L))
ggplot(xx, aes (conc, MICIT, colour = replicate)) + geom_point () +
stat_summary(geom = "line", fun = mean)
Use aes(group = 1):
ggplot(xx, aes(conc, MICIT, colour = replicate)) +
geom_point() +
geom_line() +
stat_summary(aes(group = 1), geom = "line", fun = mean)

array of correlation plots

From a mouse experiment I have data for about fifty mice coming for about 15 different metrics. I generated a list of correlation plots of every metric against every other metric to identify which measurements correlate with each other and which ones don't.
library(ggplot2)
df <- structure(list(mouse_ID = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L,
39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L,
52L, 53L, 54L, 55L), treatment = structure(c(1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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
), .Label = c("not challenged", "vehicle control", "high",
"medium", "low", "reference"
), class = "factor"), value.x = c(0.003725, 0.0208, 0.004475,
0, 0.00895, 1.00625, 1.0125, 1.014, 1.1025, 0.925, 0.897, 0.99,
1.1495, 1.0125, 1.08, 0.88425, 1.001, 0.864, 0.89175, 0.9425,
0.943, 1.07325, 0.73575, 0.606, 0.682, 0.79925, 0.87, 0.60225,
0.756, 0.891, 0.6555, 0.572, 0.253, 0.255, 0.396, 0.4495, 0.299,
0.39, 0.3, 0.5365, 0.378, 0.475, 0.73575, 0.4895, 0.468, 0.90625,
0.3905, 0.4995, 0.60375, 0.744, 0.75, 0.5535), value.y = c(0,
0, 0, 0, 0, 5.775, 4.6875, 4.992, 7.245, 6.0125, 3.795, 4.99125,
7.26275, 4.35375, 4.3875, 3.6025, 4.389, 3.852, 3.444, 4.205,
5.207, 4.77, 3.052, 2.65125, 2.024, 3.6835, 2.9, 1.5695, 2.7,
2.619, 2.964, 1.936, 0.539, 0.408, 1.056, 1.085, 0.897, 0.795,
0.5, 1.0915, 0.5355, 0.575, 2.8885, 2.0915, 1.755, 3.40625, 1.42,
1.6095, 2.835, 2.3715, 2.7, 1.927)), row.names = c(NA, -52L),
class = c("tbl_df", "tbl", "data.frame"))
ggplot(data = df, aes(x = value.x, y = value.y)) +
geom_point(aes(color = treatment)) +
geom_smooth(method = lm, se = TRUE)
#> `geom_smooth()` using formula 'y ~ x'
It turns out that a long list of over 100 plots is really hard to take in, and on each plot there is relatively little information. I would like to arrange these linear plots in a grid of the 15 x 15 measurements and visualize the correlation coefficient for the linear models by background color and overlay the linear model and data points.
Is this somehow feasible to do in ggplot? Is there another tool I could use? And if so, how should I arrange the data structure? I am comfortable dealing with purrr and nested lists for such models, but I guess in this case a long list does not seem ideal -- a matrix-style arrangement would fit the output much better.
Any thoughts or suggestions on how to approach this?
Created on 2021-01-20 by the reprex package (v0.3.0)
Sorry, my explanation wasn’t clear. The data I am showing above is only a fraction of the data available. Here I am plotting the linear correlation of two read outs. But I have over a dozen read outs that I used for pair wise comparisons. I am looking for something like this:
Each tile should be colored by a metric of the linear model (eg correlation coefficient or p value) but it should also show the graphed data and overlay of the linear model.
GGally is absolutely what I was looking for. It's simply to use and has a number of useful plotting options I will need to explore.
It turns out there are potentially some issues when the grid gets larger, bit right now it's not clear to me if this is a data issue or a limitation in the plotting function. Lot's of stuff to explore, but the simplicity of getting the first plots done is awesome.
Now to figure out how to scale the background color of each mini-plot by the overall correlation coefficient!
Are you looking for faceting?
library(ggplot2)
ggplot(df, aes(x = value.x, y = value.y)) +
geom_point(aes(color = treatment)) +
geom_smooth(method = "lm", se = TRUE) +
facet_wrap(~treatment, labeller = label_both)
If you want to compare combinations of grouping variables, try facet_grid. I'm using the builtin mtcars data for this example, since your sample data only has one categorical variable.
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
geom_smooth(method = "lm", se = TRUE) +
facet_grid(cyl ~ am, labeller = label_both)

R ggplot facet_grid_sc with cols

I'm trying to use facet_grid_sc to manipulate the y axis but by plotting the panel column-wise instead of row-wise. I have the following dataframe:
test2 <- structure(list(stream = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("Feed", "Cells 1-4",
"Cells 5-8", "Cells 9-12", "Totalconcentrate", "Tailings"), class = "factor"),
mineral = c("Calcite", "Calcite", "Calcite", "Calcite", "Scheelite",
"Scheelite", "Scheelite", "Scheelite", "Calcite", "Calcite",
"Calcite", "Calcite", "Scheelite", "Scheelite", "Scheelite",
"Scheelite", "Calcite", "Calcite", "Calcite", "Calcite",
"Scheelite", "Scheelite", "Scheelite", "Scheelite", "Calcite",
"Calcite", "Calcite", "Calcite", "Scheelite", "Scheelite",
"Scheelite", "Scheelite"), shapefactor = structure(c(3L,
1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L,
3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L, 2L,
4L), .Label = c("Angularity", "Circularity", "Formfactor",
"Roundness"), class = "factor"), mean = c(0.191074267258554,
1.57871188864644, 4.98640988695014, 0.709748496492633, 0.255307602333514,
1.41318627525434, 4.48236746482907, 0.787906844284224, 0.2370993275776,
1.59011418196729, 5.00866589220356, 0.708099932389451, 0.379279621962832,
1.41798512797767, 4.49174029724501, 0.803054249581329, 0.188107140488459,
1.58446664800185, 4.99394785197469, 0.720664938740251, 0.261663000285933,
1.33457686608134, 4.2649277507168, 0.809433325901688, 0.204386468447994,
1.55129002878455, 4.88754754288822, 0.761051008277419, 0.432222746956355,
1.22012862228623, 3.87276933395819, 0.861599941934953)), .Names = c("stream",
"mineral", "shapefactor", "mean"), row.names = c(73L, 74L, 75L,
76L, 93L, 94L, 95L, 96L, 125L, 126L, 127L, 128L, 145L, 146L,
147L, 148L, 177L, 178L, 179L, 180L, 197L, 198L, 199L, 200L, 281L,
282L, 283L, 284L, 301L, 302L, 303L, 304L), class = "data.frame")
I plot it using the following code:
scales_y <- list(
"Angularity" = scale_y_continuous(limits = c(0.5,2)),
"Circularity" = scale_y_continuous(limits = c(2,5.5)),
"Formfactor" = scale_y_continuous(limits = c(0,0.5)),
"Roundness" = scale_y_continuous(limits = c(0.6,0.9))
)
g <- ggplot(test2, aes(x=stream, y=mean, color=mineral, group=mineral))
g <- g + geom_point()
g <- g + geom_line()
g <- g + theme_bw()
g <- g + theme(axis.text.x = element_text(size =8),
axis.ticks.x=element_blank(),
legend.position="bottom")
g <- g + scale_color_brewer(palette = "Paired")
g <- g + facet_grid_sc(rows = vars(shapefactor), scales = list(y = scales_y))
print(g)
This works fine. However, if I want to plot the shapefactor in columns instead of rows (so writing facet_grid_sc(cols = vars(shapefactor), scales = list(y = scales_y))), then I get this error message:
Error in .subset2(x, i, exact = exact) : attempt to select less
than one element in get1index
I'm probably writing this wrong, but I can't find in the help of the package how to write it properly. Can anyone help me please?
Thanks in advance!
Nath
I did not get your fancy facet_grid_sc to work, but here is an alternate, a bit hack-ey solution using cowplot:
library(tidyverse)
library(cowplot)
# split, not group by for the labels
out <- test2 %>% split(.,.$shapefactor) %>%
map( ~ggplot(.,aes(x=stream, y=mean, color=mineral, group=mineral)) +
geom_point() +
geom_line() +
theme_bw() +
theme(axis.text.x = element_text(size =8),
axis.ticks.x=element_blank(),
legend.position='none') +
scale_color_brewer(palette = "Paired") +
scales_y[[.$shapefactor[1]]])
# create Dummy for legend
dummy <- ggplot(test2,aes(x=stream, y=mean, color=mineral, group=mineral)) +
geom_point() +
geom_line()+
scale_color_brewer(palette = "Paired") +
theme(legend.position = 'bottom',legend.justification = 'center')
# add legend to list
out$' ' <- cowplot::get_legend(dummy)
cowplot::plot_grid(plotlist = out, ncol = 1,labels = names(out),axis = 'r', align = 'h')
It obviously needs a bit of formatting, but you get the jest.
plot_grid offers a lot of customizability for its labels, the legend has to be changed via the dummy-plot.

Plotting multiple effect plots from logistic regression

I have a number of logistic regression models with different response variables but the same predictor variables. I want to use grid.arrange (or anything else) to make a single figure with all these effect plots that were made with the effects package. I followed the advice here to make such a graph: grid.arrange with John Fox's effects plots
library(effects)
library(gridExtra)
data <- structure(list(group = structure(c(1L, 1L, 1L, 1L, 1L,1L, 1L, 2L, 2L, 2L), .Label = c("group1", "group2"), class = "factor"),obs = c(1L, 1L, 4L, 4L, 6L, 12L, 26L, 1L, 10L, 6L),responseA = c(1L, 1L, 2L, 0L, 1L, 10L, 20L, 0L, 3L, 2L), responseB = c(0L, 0L, 2L, 4L, 6L, 4L, 8L, 1L, 8L, 5L)), .Names = c("group", "obs", "responseA","responseB"), row.names = c(53L, 54L, 55L, 56L, 57L, 58L,59L, 115L, 116L, 117L), class = "data.frame")
model1<-glm(cbind(responseA,(obs-responseA))~group,family=binomial, data=data)
model2<-glm(cbind(responseA,(obs-responseA))~group,family=binomial, data=data)
ef1 <-allEffects(model1)[[1]]
ef2 <- allEffects(model2)[[1]]
elist <- list( ef1,ef2)
class(elist) <- "efflist"
plot(elist, col=2)
The problem is that, in the models I am using the response variable in the model in the form cbind(response A,no response A), but for the figure I would like to change it to something more clean (like Response A). I tried changing the y labels by putting a list, but got a warning, and it turned both labels into "Response A".
plot(elist, ylab=c("response A","response B"),col=2)
Then tried the second method suggestion to change the class to trellis, got an error, so grid.arrange didn’t work either.
p1<-plot(allEffects(model1),ylab="Response A")
p2<-plot(allEffects(model2),ylab="Response B")
class(p1) <- class(p2) <- "trellis"
grid.arrange(p1, p2, ncol=2)
Can anyone provide a method to change each y-axis label separately?
With the ef1 and ef2 variables you created, you can try the following
plot1 <- plot(ef1, ylab = "Response A")
plot2 <- plot(ef2, ylab = "Response B")
grid.arrange(plot1, plot2, ncol=2)

Smoothing in ggplot

I have this ggplot
ggplot(dt.1, aes(x=pctOAC,y=NoP, fill=Age)) +
geom_bar(stat="identity",position=position_dodge()) +
geom_smooth(aes(x=pctOAC,y=NoP, colour=Age), se=F, method="loess",show_guide = FALSE,lwd=0.7) +
theme(legend.position=c(.2,0.8))
dt1 <- structure(list(Age = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("o80", "u80"), class = "factor"), NoP = c(47L, 5L, 33L, 98L, 287L, 543L, 516L, 222L, 67L, 14L, 13L, 30L, 1L, 6L, 17L, 30L, 116L, 390L, 612L, 451L, 146L, 52L), pctOAC = c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)), .Names = c("Age", "NoP", "pctOAC"), row.names = c(NA, -22L), class = "data.frame")
I would like to have the smooth lines constrained to lie above zero, perhaps something similar to a kernel density. In fact if I had the underlying data, I expect a kernel density is exactly what I would want, but I only have the aggregated data. Is there any way to do this ? I tried using different method= in the geom_smooth, but the small dataset seems to prevent it. I wondered about using stat_function but I don't have much clue about how to proceed with finding a suitable function to plot.
Another possibility is to use method="glm" with a spline curve and a log link (i.e. also tried method="gam", but its automatic complexity adjustment wanted to reduce the wiggliness too much:
library(splines)
ggplot(dt.1, aes(x=pctOAC,y=NoP, fill=Age)) +
geom_bar(stat="identity",position=position_dodge()) +
geom_smooth(aes(colour=Age), se=F,
method="glm",
formula=y~ns(x,8),
family=gaussian(link="log"),
show_guide = FALSE,lwd=0.7) +
theme(legend.position=c(.2,0.8))
How about geom_density()?
ggplot(dt1, aes(x=pctOAC,y=NoP, colour=Age, fill=Age)) +
geom_bar(stat="identity",position=position_dodge()) +
geom_density(stat="identity", fill=NA) +
theme(legend.position=c(.2,0.8))

Resources