Smoothing in ggplot - r

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

Related

not enough 'x' or 'y' observations in ggplot2 for t-tests or Wilcoxon test

I'm able to run a t-test or a Wilcoxon test on the data with no warnings, but I get an error when I try to plot in with ggpubr 's function stat_pvalue_manual() in ggplot2
The t-test works fine:
### running a t-test (no problem):
t.test(SUJ_PRE ~ AMOSTRA, data = data, exact = F)
But I get the error in stat_pvalue_manual:
### trying to plot it:
data %>%
ggplot(., aes(x = AMOSTRA, y = SUJ_PRE)) +
stat_boxplot(geom = "errorbar",
width = 0.15) +
geom_boxplot(aes(fill = AMOSTRA), outlier.colour = "#9370DB", outlier.shape = 19,
outlier.size= 2, notch = T) +
scale_fill_manual(values = c("PB" = "#E6E6FA", "PE" = "#CCCCFF"),
label = c("PB" = "PB", "PE" = "PE"),
name = "Amostra:") +
stat_summary(fun = mean, geom = "point", shape = 20, size= 5, color= "#9370DB") +
stat_pvalue_manual(data %>%
t_test(SUJ_PRE ~ AMOSTRA) %>%
add_xy_position(),
label = "t = {round(statistic, 2)}, p = {p}")
Error in `mutate()`:
! Problem while computing `data = map(.data$data, .f, ...)`.
Caused by error in `t.test.default()`:
! not enough 'x' observations
With SUJ_PRE I'm getting the error with 'x' , but I've also got the 'y' message with another variable too. Any thoughts on that? I've seem some similar questions, but I couldn't solve my problem. Thanks in advance.
data:
> dput(data)
structure(list(ID = c("COPA_M1B", "COPA_M1C", "COPA_M2B", "COPA_M2C",
"COPA_M3C", "COPA_M3A", "COPA_M3B", "COPA_H1A", "COPA_H1B", "COPA_H2A",
"COPA_H2B", "COPA_H2C", "COPA_H3A", "COPA_H3B", "NI_M1B", "NI_M1C",
"NI_M2A", "NI_M2B", "NI_M3C", "NI_M3A", "NI_M3B", "NI_H1A", "NI_H1B",
"NI_H1C", "NI_H2B", "NI_H2C", "NI_H3A", "NI_H3C", "CACEM_M1A",
"CACEM_M1B", "CACEM_M1C", "CACEM_M2B", "CACEM_M3B", "CACEM_H1B",
"CACEM_H1C", "CACEM_H2A", "CACEM_H2C", "OEIRAS_M1B", "OEIRAS_M1C",
"OEIRAS_M2B", "OEIRAS_M3B", "OEIRAS_M3C", "OEIRAS_H1B"), SUJ_PRE = c(25,
40, 56, 49, 47, 38, 58, 38, 42, 71, 43, 46, 74, 43, 45, 35, 70,
33, 45, 53, 50, 59, 62, 41, 35, 43, 40, 21, 23, 33, 35, 21, 36,
15, 31, 19, 31, 20, 22, 20, 19, 21, 25), AMOSTRA = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("PB", "PE"
), class = "factor")), class = c("grouped_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -43L), groups = structure(list(
ID = c("CACEM_H1B", "CACEM_H1C", "CACEM_H2A", "CACEM_H2C",
"CACEM_M1A", "CACEM_M1B", "CACEM_M1C", "CACEM_M2B", "CACEM_M3B",
"COPA_H1A", "COPA_H1B", "COPA_H2A", "COPA_H2B", "COPA_H2C",
"COPA_H3A", "COPA_H3B", "COPA_M1B", "COPA_M1C", "COPA_M2B",
"COPA_M2C", "COPA_M3A", "COPA_M3B", "COPA_M3C", "NI_H1A",
"NI_H1B", "NI_H1C", "NI_H2B", "NI_H2C", "NI_H3A", "NI_H3C",
"NI_M1B", "NI_M1C", "NI_M2A", "NI_M2B", "NI_M3A", "NI_M3B",
"NI_M3C", "OEIRAS_H1B", "OEIRAS_M1B", "OEIRAS_M1C", "OEIRAS_M2B",
"OEIRAS_M3B", "OEIRAS_M3C"), .rows = structure(list(34L,
35L, 36L, 37L, 29L, 30L, 31L, 32L, 33L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 1L, 2L, 3L, 4L, 6L, 7L, 5L, 22L,
23L, 24L, 25L, 26L, 27L, 28L, 15L, 16L, 17L, 18L, 20L,
21L, 19L, 43L, 38L, 39L, 40L, 41L, 42L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -43L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE))
Your data frame is grouped by ID, and rstatisx::t_test will honor the groupings in a data frame. This means some groups only have a single member, which of course causes an error.
The solution is simply to ungroup your data frame when using it inside stat_pvalue_manual:
data %>%
ggplot(aes(x = AMOSTRA, y = SUJ_PRE)) +
stat_boxplot(geom = "errorbar",
width = 0.15) +
geom_boxplot(aes(fill = AMOSTRA), outlier.colour = "#9370DB",
outlier.shape = 19,
outlier.size= 2, notch = T) +
scale_fill_manual(values = c("PB" = "#E6E6FA", "PE" = "#CCCCFF"),
label = c("PB" = "PB", "PE" = "PE"),
name = "Amostra:") +
stat_summary(fun = mean, geom = "point", shape = 20, size= 5,
color = "#9370DB") +
stat_pvalue_manual(data %>% ungroup() %>%
t_test(SUJ_PRE ~ AMOSTRA) %>%
add_xy_position(),
label = "t = {round(statistic, 2)}, p = {p}")

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)

Can I making a grouped barplot for percentages in R using ggplot?

This sounds like a popular plot but I really was trying to figure it out without any solution! Can I produce a plot that shows the percentage of the occurrence in each Blocked lanes inside each Duration? My data is
data<- structure(list(Lanes.Cleared.Duration = c(48, 55, 20, 38, 22,
32, 52, 21, 39, 14, 69, 13, 14, 13, 25), Blocked.Lanes = c(1L,
2L, 1L, 2L, 5L, 3L, 3L, 1L, 3L, 2L, 2L, 2L, 2L, 3L, 1L), Durations = structure(c(3L,
3L, 2L, 3L, 2L, 3L, 3L, 2L, 3L, 2L, 4L, 2L, 2L, 2L, 2L), .Label = c("<10",
"<30", "<60", "<90", "<120", ">120"), class = "factor")), row.names = c(NA,
-15L), na.action = structure(c(`17` = 17L, `26` = 26L, `28` = 28L,
`103` = 103L, `146` = 146L, `166` = 166L, `199` = 199L, `327` = 327L,
`368` = 368L, `381` = 381L, `431` = 431L, `454` = 454L, `462` = 462L,
`532` = 532L, `554` = 554L, `703` = 703L, `729` = 729L, `768` = 768L,
`769` = 769L, `785` = 785L, `970` = 970L, `1043` = 1043L, `1047` = 1047L,
`1048` = 1048L, `1081` = 1081L, `1125` = 1125L), class = "omit"), class = "data.frame")
I tried the following code and it gave me Real Duration rather than percentage. Here is my code.
data %>%
ggplot(aes(fill=factor(Blocked.Lanes), y=Lanes.Cleared.Duration, x=Durations)) +
geom_bar(position="dodge", stat="identity")
My result should show the percentage of occurrence of each Blocked lane inside each Duration.
I tried to group by Durations but it did not work.
Not quite elegant, but you can do a tally by duration and blocked lane first, and then do a percentage with grouped duration.
df1 <- data %>% group_by(Durations, Blocked.Lanes) %>% tally()
df1 <- df1 %>% ungroup %>% group_by(Durations) %>% mutate(perc = n/sum(n))
ggplot(df1, aes(fill=factor(Blocked.Lanes), y=perc, x=Durations)) +
geom_bar(position="dodge", stat="identity")
You can do:
library(tidyverse)
data %>%
count(Durations, Blocked.Lanes) %>%
group_by(Durations) %>%
mutate(n = prop.table(n) * 100) %>%
ggplot(aes(fill = factor(Blocked.Lanes), y = n, x = Durations)) +
geom_bar(position = "dodge", stat = "identity") +
ylab("Percentage of Blocked Lane") +
guides(fill = guide_legend(title = "Blocked Lane"))
Output

What is the best way to use agricolae to do ANOVAs on a split plot design?

I'm trying to run some ANOVAs on data from a split plot experiment, ideally using the agricolae package. It's been a while since I've taken a stats class and I wanted to be sure I'm analyzing this data correctly, so I did some searching online and couldn't really find consistency in the way people were analyzing their split plot experiments. What is the best way for me to do this?
Here's the head of my data:
dput(head(rawData))
structure(list(ï..Plot = 2111:2116, Variety = structure(c(5L,
4L, 3L, 6L, 1L, 2L), .Label = c("Burbank", "Hodag", "Lamoka",
"Norkotah", "Silverton", "Snowden"), class = "factor"), Rate = c(4L,
4L, 4L, 4L, 4L, 4L), Rep = c(1L, 1L, 1L, 1L, 1L, 1L), totalTubers = c(594L,
605L, 656L, 729L, 694L, 548L), totalOzNoCulls = c(2544.18, 2382.07,
2140.69, 2401.56, 2440.56, 2503.5), totalCWTacNoCulls = c(461.76867,
432.345705, 388.535235, 435.88314, 442.96164, 454.38525), avgLWratio = c(1.260615419,
1.287949374, 1.111981583, 1.08647584, 1.350686661, 1.107173509
), Hollow = c(14L, 15L, 22L, 25L, 14L, 13L), Double = c(10L,
13L, 15L, 22L, 11L, 9L), Knob = c(86L, 80L, 139L, 156L, 77L,
126L), Researcher = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Wang", class = "factor"),
CullsPounds = c(1.75, 1.15, 4.7, 1.85, 0.8, 5.55), CullsOz = c(28,
18.4, 75.2, 29.6, 12.8, 88.8), totalOz = c(2572.18, 2400.47,
2215.89, 2431.16, 2453.36, 2592.3), totalCWTacCulls = c(466.85067,
435.685305, 402.184035, 441.25554, 445.28484, 470.50245)), row.names = c(NA,
6L), class = "data.frame")
For these data, the whole plot is Rate, the split plot is Variety, the block is Rep, and for discussion's sake here, we can look at totalCWTacNoCulls as the response.
Any help would be very much appreciated! I am still getting the hang of Stack Overflow, so if I have made any mistakes or shared my data wrong, please let me know and I'll change it. Thank you!
You can do this using agricolae package as follows
library(agricolae)
attach(rawData)
Rate = factor(Rate)
Variety = factor(Variety)
Rep = factor(Rep)
sp.plot(Rep, Rate, Variety, totalCWTacNoCulls)
Usage according to agricolae package is
sp.plot(block, pplot, splot, Y)
where, block is replications, pplot is main-plot Factor, splot is sub-plot Factor and Y response variable

Drawing SE in xyplot with errorbars

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

Resources