I have been using variations of the following code for quite a while, but now I am working with a very small dataset (28 rows) and that gives me two different problems: the first one is that the edge effects seem to be massive doesn't matter what kernel I use. The second one is not exactly a problem, but for what I am trying to do now it would be convenient to be able to show counts instead of density in the y-axis. I know it can be done as I have seen density plots without edge effect problems (or at least they are not that apparent) and with counts in the y-axis in publications. I have been looking at how to solve my problems for days and either I have found nothing or I have not been able to implement what I have found. It seems that my problems would be easier to solve if I were using ggplot2, but a part of the code was made by a former teacher of mine and I just don't how to reproduce what he did in ggplot2. Anyway, here is my code:
bw = 500
y <- density(check$sim, bw = bw, n=512, kernel = "gaussian", weights = burialsToRun$Weight)
ymax=0.010
par(mar=c(4.1,2.1,2.1,6.1))
plot(y, xlim = c(-9500, -3500), xaxs = "i", ylim = c(0, ymax), main="", col = rgb(0,0,0,0.05), axes = FALSE, ylab = " ", xlab = " ")
axis(1, at=seq(-9500, -3500, by = 500), las = 1)
axis(4, at=seq(0, ymax, by = 0.001), las = 1)
mtext(text="Density", side=4, line=4, las=3)
mtext(text="Calibrated date (cal. BC)", side=1, line=2.5, las=1)
box() # para meter mi gráfico en una cajita
for(i in 1:3000) {
burialsToRun[, sim := {x <- runif(nrow(burialsToRun)); (x * (End - Start)) + Start}]
check <- burialsToRun[, list(Start, End, sim)]
y <- density(check$sim, bw = bw, n=512, kernel = "gaussian", weights = burialsToRun$Weight)
lines(y, col= rgb(0,0,0,0.05))
}
Data:
check <- structure(list(Start = c(-7060, -8174, -7530, -5300, -5300, -5881,
-5600, -5600, -7000, -9300, -9200, -9300, -9300, -6200, -4240,
-4240, -4240, -4240, -4240, -4240, -8999, -5208, -5979, -5476,
-5979, -5630, -5470, -5979), End = c(-6246, -7833, -7201, -3950,
-3950, -5747, -5000, -5000, -5500, -9200, -8300, -8500, -8300,
-5600, -3480, -3480, -3480, -3480, -3480, -3480, -5000, -4840,
-4333, -5029, -4333, -4333, -4452, -5533), sim = c(-6407.1891290769,
-7845.75364494463, -7345.1619859545, -4872.66194844851, -4995.94754445134,
-5802.87022047071, -5151.49392490275, -5135.55407933891, -6972.37427707296,
-9291.56357476022, -8720.51239821594, -8767.55619421601, -9271.44779283553,
-6083.65002772771, -4060.82516569644, -3753.49776098505, -4222.84405510873,
-4165.94615798444, -4007.33239018358, -4069.03453745879, -5708.78666541586,
-5171.84607211128, -5616.71954697417, -5134.90122210816, -5173.40105968062,
-4721.55411865073, -4935.3219843721, -5927.93708416494)), row.names = c(NA,
-28L), class = c("data.table", "data.frame"))
burialsToRun <- structure(list(Burial = 1:28, Start = c(-7060, -8174, -7530,
-5300, -5300, -5881, -5600, -5600, -7000, -9300, -9200, -9300,
-9300, -6200, -4240, -4240, -4240, -4240, -4240, -4240, -8999,
-5208, -5979, -5476, -5979, -5630, -5470, -5979), End = c(-6246,
-7833, -7201, -3950, -3950, -5747, -5000, -5000, -5500, -9200,
-8300, -8500, -8300, -5600, -3480, -3480, -3480, -3480, -3480,
-3480, -5000, -4840, -4333, -5029, -4333, -4333, -4452, -5533
), Weight = 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
), sim = c(-6804.49034169316, -7978.35553480918, -7266.07094835793,
-5021.92656769184, -3969.11897005048, -5871.17116243485, -5130.67335872911,
-5569.97352014296, -5871.00151088089, -9211.72905953135, -8927.66143891495,
-8923.21154009551, -9210.72582732886, -5775.25383243337, -3504.35795384459,
-3780.76538468711, -4138.03785683587, -3826.4371876698, -3882.20702474937,
-4058.1953032501, -5341.79313169955, -4980.0766399838, -4356.67793130036,
-5386.85334781208, -5036.12725707935, -5213.0496873688, -4550.98105533235,
-5877.78668494383)), row.names = c(NA, -28L), class = c("data.table",
"data.frame"))
Since the solutions I found were for ggplot2, what I have been trying is, first of all, to make the same plot in ggplot2.
ggplot(check, aes(sim, after_stat(count))) +
stat_density(mapping = NULL, data = NULL, geom = "line", position = "stack", bw = 500, adjust = 1, kernel = "gaussian", trim = FALSE, na.rm = FALSE, bounds = c(-9500, -3500), orientation = NA, show.legend = NA, inherit.aes = TRUE) +
xlim(-9500, -3500) +
xlab("Calibrated date (cal. BC)") +
ylab("Count") +
theme_classic() +
theme(plot.margin = unit(c(1, 1, 1, 1), "lines")) +
scale_x_continuous(breaks = seq(-9500, -3500, by = 1000)) +
scale_y_continuous(position = "right")
As you can see, I did not get far and what I did is probably wrong. I certainly do not know how to make the 3000 repetitions I need.
I would prefer if the problems could be directly solved using the plot function rather than ggplot2, since I am used to that one. However, if that is not possible I would be glad if someone could explain how to make the 3000 repetitions in ggplot2. Also, please, point if there is something wrong with what I already made. I hope someone can help. Thanks in advance!
Related
I'm having trouble creating an interaction effect plot. There is probably something fairly simple I don't yet know how to do. I'm pretty new to R and ggplot. My reprex is below. Your insight is greatly appreciated!
The data is from UCLA and I'm also adapting their example for my purposes here.
library(here)
library(emmeans)
library(tidyverse)
dat <- read.csv("https://stats.idre.ucla.edu/wp-content/uploads/2019/03/exercise.csv")
Convert prog into factor variable
dat$prog <- factor(dat$prog, labels = c("jog","swim","read"))
The model
contcat <- lm(loss ~ hours * prog, data=dat)
summary(contcat)
I create mylist with certain points on hours and the two categories in prog that I want to contrast.
(mylist <- list(hours = seq(0, 4, .5), prog=c("jog","read")))
I then pass the object contcat into the emmeans. I request that predicted values of every combination of hours and prog be specified in at=mylist and store the output into an object called emcontcat.
emcontcat <- emmeans(contcat, ~ hours * prog, at=mylist)
I use emmip to output a set of values using plotit=FALSE.
contcatdat <- emmip(contcat, prog ~ hours, at = mylist, CIs=TRUE, plotit=FALSE)
The output object is fed to ggplot. The interaction effect is plotted along with CI bands.
ggplot(data=contcatdat, aes(x=hours, y=yvar, color=prog)) +
geom_line() +
geom_ribbon(aes(ymax=UCL, aymin=LCL, fill=prog), alpha=0.4)
The plot looks like this:
But overlapping CIs do not always correspond to the portions of the lines where there is no significant differences in predicted values. I want to add hashed lines for the portions of the lines where there is no significant difference in predicted values. This figure below
shows the kind of figure I'm trying to create. (The figure is from a paper by Trenton Mize (2019) found here at Fig. 14.)
To get the simple effect (i.e., difference of two predicted values), I pass emcontcat into a function called contrast where we can request "pairwise" differences (or simple effects). P-values are given for jog - read at each level of hours that was specified in mylist.
contrast(emcontcat, "pairwise", by="hours")
The output:
Where I am having trouble is how to incorporate the simple effect (i.e., the parts of hours where jog - read are significantly different or not) into ggplot as hashed or solid portions of the lines like the Mize 2019 figure.
We want to know if the intervals overlap, and if so, we want dashed lines. Actually that's easy by writing a respective function itvl_is_l(). However, on the LHS of the plot, there is just one point, but to draw a line we need a minimum of two. So we have to interpolate with "approximate", which is also done internally in the plot functions. Since we want to do everything for the two progs, we use by.
Preprocessing
## merge interpolations by prog
aux <- by(contcatdat, contcatdat$prog, \(x) {
x <- merge(x, data.frame(hours=with(x, seq.int(min(hours), max(hours),
length.out=1e3))), all=TRUE)
x$prog <- unique(na.omit(x$prog))
u <- c('yvar', 'LCL', 'UCL')
x[u] <- lapply(x[u], \(x) approx(x, xout=seq_along(x))$y)
x
})
## logical interval intersect function
itvl_is_l <- \(a, b) {unname(as.vector(ifelse(b[, 1] > a[, 2] | a[, 1] > b[2], TRUE, FALSE)))}
## check if intersecting CIs
its <- itvl_is_l(aux$jog[c('LCL', 'UCL')], aux$read[c('LCL', 'UCL')])
aux <- lapply(aux, `[<-`, 'its', val=its) ## add as variable
aux <- lapply(aux, \(x) transform(x, itsn=cumsum(c(0, diff(x$its)) != 0) + 1)) ## making a sequence out of it
contcatdat <- do.call(rbind, aux) ## combine back as contcatdat
Plot
clr <- c('#FF0000', '#0000FF', '#0000001A') ## some colors
png('foo.png', 600, 400) ## open .png device
plot(yvar ~ hours, contcatdat, type='n')
grid()
## lines left
lines(yvar ~ hours, contcatdat, subset=prog == 'jog' & itsn > 2, lwd=2, col=clr[1])
lines(yvar ~ hours, contcatdat, subset=prog == 'read' & itsn > 2, lwd=2, col=clr[2])
## lines middle, dashed
lines(yvar ~ hours, contcatdat, subset=prog == 'jog' & itsn == 2, lwd=2, col=clr[1], lty=2)
lines(yvar ~ hours, contcatdat, subset=prog == 'read' & itsn == 2, lwd=2, col=clr[2], lty=2)
## lines right
lines(yvar ~ hours, contcatdat, subset=prog == 'jog' & itsn < 2, lwd=2, col=clr[1])
lines(yvar ~ hours, contcatdat, subset=prog == 'read' & itsn < 2, lwd=2, col=clr[2])
## CIs
with(subset(contcatdat, prog == 'jog'),
polygon(c(hours, rev(hours)), c(UCL, rev(LCL)), border=NA, col=clr[3]))
with(subset(contcatdat, prog == 'read'),
polygon(c(hours, rev(hours)), c(UCL, rev(LCL)), border=NA, col=clr[3]))
## legend
legend('topleft', legend=unique(contcatdat$prog), title='Group', col=clr[1:2], lty=1, lwd=2)
dev.off() ## close .png device
You could also try to plot the polygons first and opaque with a border, if that might look better.
Data:
contcatdat <- structure(list(prog = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), levels = c("jog",
"read"), class = "factor"), hours = c(0, 0, 0.5, 0.5, 1, 1, 1.5,
1.5, 2, 2, 2.5, 2.5, 3, 3, 3.5, 3.5, 4, 4), yvar = c(-6.78065983345649,
2.21637209230689, -3.05428518360714, 0.738291278604121, 0.672089466242214,
-0.739789535098646, 4.39846411609157, -2.21787034880141, 8.12483876594092,
-3.69595116250418, 11.8512134157903, -5.17403197620695, 15.5775880656396,
-6.65211278990971, 19.303962715489, -8.13019360361248, 23.0303373653383,
-9.60827441731525), SE = c(1.64384530410457, 1.48612021916972,
1.25520349531108, 1.14711211184156, 0.87926401607137, 0.820840725755632,
0.543079708493216, 0.531312719216624, 0.375535476484592, 0.376041650300328,
0.558013604603198, 0.501120592808483, 0.89777081499028, 0.781944232621328,
1.27470257475094, 1.1056003463909, 1.66373129934114, 1.44356083265185
), df = c(894, 894, 894, 894, 894, 894, 894, 894, 894, 894, 894,
894, 894, 894, 894, 894, 894, 894), LCL = c(-10.0069052579393,
-0.700318757711651, -5.51777400669205, -1.51305511813823, -1.05357261502514,
-2.35078883599747, 3.33260443922245, -3.26063588462286, 7.38780492844162,
-4.43397842739773, 10.7560441598055, -6.15754180868669, 13.815604150934,
-8.18677301395645, 16.8022045883112, -10.3000681349591, 19.7650632676689,
-12.4414373187615), UCL = c(-3.55441440897366, 5.13306294232543,
-0.590796360522233, 2.98963767534648, 2.39775154750957, 0.871209765800175,
5.46432379296068, -1.17510481297997, 8.86187260344022, -2.95792389761063,
12.946382671775, -4.19052214372721, 17.3395719803452, -5.11745256586298,
21.8057208426668, -5.96031907226584, 26.2956114630078, -6.77511151586902
), tvar = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), levels = c("jog", "read"), class = "factor"),
xvar = c(0, 0, 0.5, 0.5, 1, 1, 1.5, 1.5, 2, 2, 2.5, 2.5,
3, 3, 3.5, 3.5, 4, 4)), estName = "yvar", clNames = c("lower.CL",
"upper.CL"), pri.vars = c("prog", "hours"), adjust = "none", side = 0, delta = 0, type = "link", mesg = "Confidence level used: 0.95", row.names = c(NA,
18L), class = c("summary_emm", "data.frame"), labs = list(xlab = "hours",
ylab = "Linear prediction", tlab = "prog"), vars = list(byvars = character(0),
tvars = "prog"))
I have an issue with plotting continuous distributions without sharp edges in ggplot2. I need to show two of them on one plot. Also, it does not have to be ggplot2 to achieve this result.
I am aware, that the number of data points directly influences the smoothness of the line, but it was not the case here. Below you can see some sample data (from dput)
sample.data<-list(beta.data = structure(list(cluster = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), beta.density = c(0, 3.42273368363934e-43, 8.42987148403021e-29,
2.04764468657484e-20, 1.69485562831516e-14, 6.07999638837842e-10, 2.88180370232676e-06, 0.00314064636750876, 0.954118897015866, 0, 0, 3.80101893822358e-36, 6.43342582657081e-22, 6.82956252277493e-14, 1.75291058931833e-08, 0.000131874335695378, 0.0750918340641428, 3.72532418759802, 5.05242078519544, 0), pr = c(0, 0.111111111111111, 0.222222222222222, 0.333333333333333, 0.444444444444444, 0.555555555555556, 0.666666666666667, 0.777777777777778, 0.888888888888889, 1, 0, 0.111111111111111, 0.222222222222222, 0.333333333333333, 0.444444444444444, 0.555555555555556, 0.666666666666667, 0.777777777777778, 0.888888888888889, 1)), row.names = c(NA, -20L), class = "data.frame"), beta.params = structure(list(cluster = 1:2, a = c(49, 50), b = c(2, 10), ni.beta = c(0.961,0.833), sd.beta = c(0.00072, 0.00228)), row.names = c(NA,-2L), class = "data.frame"))
Before I was using geom_col, but it discretizes values. I went with geom_area:
ggplot(sample.data$beta.data, aes(x = pr, y = beta.density)) +
geom_area(stat = "function",
fun = dbeta,
args = list(shape1 = sample.data$beta.params[1,"a"], shape2 = sample.data$beta.params[1,"b"]),
fill = "#F1C40F",
colour = "black",
alpha = 0.7) +
geom_area(stat = "function",
fun=dbeta,
args = list(shape1 = sample.data$beta.params[2,"a"], shape2 = sample.data$beta.params[2,"b"]),
fill = "#3498DB",
colour = "black",
alpha = 0.7)
I presented you the data with 10 points, but 1000 points look almost the same. It is not the case here, where even 100 points looks ok:
p = seq(0,1, length=100)
plot(p, dbeta(p, 50, 10), ylab="Density", type ="l", col=4, , lwd = 2)
Here I am attaching code to simulate the data. Oh, and these troublesome beta parameters were a = 49 and b = 2.
len <- 100
p <- seq(0,1, length.out = len)
df <- data.frame(rbind(cbind("cl" = rep(1, times = length(p)), "beta" = dbeta(p, 50, 10),"p"= p),
cbind("cl" = rep(1, times = length(p)), "beta" = dbeta(p, 40, 2),"p"= p)))
Do you have any ideas?
EDIT: The pdfs stands here for probability density functions. That is why I have not put "pdf" as a tag. My apologies for the confusion!
Anyway, when I tried to print graphic to PDF file, the result was poor as well (sharp edges). But it the end, it shouldn't matter. I want to see smooth lines whatever I do (reasonably).
EDIT2 It is possible to achieve because:
library(mosaic)
theme_set(theme_bw())
xpbeta(c(0.7, 0.90), shape1 = 49, shape2 = 2)
It produces nice, smoothed beta dist with parameters (49, 2). But then again, I need to show two dists in one chart.
I have found the answer. It still needs some editing (like transparency/alpha which I couldn't figure out), but in general, this is what I meant. Code:
library(mosaic)
plotDist('beta', params=list(49,2), kind='density', type = "h", col = "#3498DB", xlim = c(0,1))
plotDist('beta', params=list(50, 10), kind='density', , type = "h", col = "#F1C40F", add = TRUE)
plotDist('beta', params=list(49,2), kind='density', add = TRUE, col = "black")
plotDist('beta', params=list(50, 10), kind='density', add = TRUE, col = "black")
Result:
We can add as many distributions as we want, using "add" parameter.
Parameter type = "h", is used to draw filled distribution. Without it, the only line is visible. In my answer, I draw the two lines and two filled dists. I would be really happy if someone could show a better answer, though.
EDIT:
I think I found my perfect answer!
Here is the code:
library(ggformula)
theme_set(theme_bw())
gf_dist("beta", shape1 = 49, shape2 = 2, geom = "area", alpha = 0.5, fill = "#F1C40F") %>%
gf_dist("beta", shape1 = 49, shape2 = 2) %>%
gf_dist("beta", shape1 = 50, shape2 = 10, geom = "area", alpha = 0.5, fill = "#3498DB") %>%
gf_dist("beta", shape1 = 50, shape2 = 10)
It is much faster than the previous code, parameter alpha is obvious and it is relatively easy to combine many plots! Because of transparency, you can nicely see the overlap of both distributions.
I have been working on this for some time, and am re-posting this hoping to simplify the definition of the problem and to bring some clarity from feedback of my previous attempt. I am able to label each individual column value, but not able to put the code together necessary to sum the total. The examples I have looked at never work the way I try to put them together, for example with goup_by, or summarize etc.. I would like to only sum the values of "Confirmed Cases", and not show the other column values as with many c("x", "Y", ... "data"), it becomes impossible to read.
Here is the data frame:
dput(COVID1[1:12, ])
structure(list(COUNTY = c("Antrim", "Antrim", "Antrim", "Charlevoix",
"Charlevoix", "Grand Traverse", "Grand Traverse", "Grand Traverse",
"Antrim", "Grand Traverse", "Grand Traverse", "Grand Traverse"
), Date = structure(c(18453, 18456, 18457, 18453, 18455, 18453,
18456, 18457, 18455, 18453, 18456, 18457), class = "Date"), CASE_STATUS = c("Confirmed",
"Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed",
"Confirmed", "Confirmed", "Probable", "Probable", "Probable",
"Probable"), Cases = c(1L, 1L, 2L, 1L, 3L, 2L, 2L, 1L, 1L, 1L,
1L, 1L)), row.names = c(NA, 12L), class = "data.frame")
Code:
ggplot(filter(COVID1, COUNTY %in% c("Antrim", "Charlevoix", "Grand Traverse"), Cases > 0)) +
geom_col(aes(x = Date, y = Cases, fill = CASE_STATUS), position = position_stack(reverse = TRUE), width = .88)+
geom_text(aes(x = Date, y = Cases, label = (Cases)), position = position_stack(reverse = TRUE), vjust = 1.5, size = 3, color = "white") +
scale_fill_manual(values = c('blue',"tomato"))+
scale_x_date(labels = date_format("%m/%d"), limits = as.Date(c('2020-07-09','today()')), breaks = "1 week")+
theme(axis.text.x = element_text(angle=0))+
labs(title = "Antrim - Grand Traverse - Charlevoix")
I'm not sure if I understood the question but I think you want to add the sum of the confirmed cases as labels. There might be a ggplot way of doing it but I think the most straightforward way is to make another dataset with your labels and feed it in.
date_labels <- filter(COVID1, COUNTY %in% c("Antrim", "Charlevoix", "Grand Traverse"), Cases > 0) %>% group_by(Date) %>% summarise(confirmed_cases = sum(Cases[CASE_STATUS == "Confirmed"]))
ggplot(filter(COVID1, COUNTY %in% c("Antrim", "Charlevoix", "Grand Traverse"), Cases > 0)) +
geom_col(aes(x = Date, y = Cases, fill = CASE_STATUS), position = position_stack(reverse = TRUE), width = .88)+
geom_text(data = date_labels, aes(x = Date, y = 1, label = confirmed_cases), position = position_stack(reverse = TRUE), vjust = 1.5, size = 3, color = "white") +
scale_fill_manual(values = c('blue',"tomato"))+
scale_x_date(labels = label_date("%m/%d"), limits = as.Date(c('2020-07-09','today()')), breaks = "1 week")+
theme(axis.text.x = element_text(angle=0))+
labs(title = "Antrim - Grand Traverse - Charlevoix")
Gives me this result:
When I plot the dendrogram using "as.dendrogram" all the leaf labels are cut.
See those linked images below as examples:
When I plot using "hclust" (all labels showing correctly)
Dendrogram 1
When I plot using "as.dendrogram"
Dendrogram 2
Codes:
The data I created just to show the problem
test <- matrix(c(34,34,32,27,12,1,2,1,1,1), ncol=2)
colnames(test) <- c('Variable1', 'Variable2')
rownames(test) <- c('African Forest Elephant', 'Asian Domestic Elephant','African White Rhino','West African Giraffe','African Mountain Gorilla')
test_table <- as.table(test)
To plot the first linked image
hctest = hclust(dist(test_table))
plot(hctest, axes = FALSE)
The second one
hctest = as.dendrogram(hctest)
plot(hctest, edge.root = TRUE, horiz = TRUE)
PS: If I use "horiz = FALSE" the same problem persists. I tried exporting the image and also tried using R markdown, but nothing changes.
You can fix this with par by adjusting the plot margins mar=c(bottom, left, top, right). (Defaults are c(5, 4, 4, 2) + 0.1).
par(mar=c(5, 4, 4, 10) + 0.1)
plot(hctest, edge.root=TRUE, horiz=TRUE)
Data
hctest <- structure(list(structure(5L, members = 1L, height = 0, label = "African Mountain Gorilla", leaf = TRUE),
structure(list(structure(4L, members = 1L, height = 0, label = "West African Giraffe", leaf = TRUE),
structure(list(structure(3L, members = 1L, height = 0, label = "African White Rhino", leaf = TRUE),
structure(list(structure(1L, label = "African Forest Elephant", members = 1L, height = 0, leaf = TRUE),
structure(2L, label = "Asian Domestic Elephant", members = 1L, height = 0, leaf = TRUE)), members = 2L, midpoint = 0.5, height = 1)), members = 3L, midpoint = 0.75, height = 2.23606797749979)), members = 4L, midpoint = 0.875, height = 7.07106781186548)), members = 5L, midpoint = 0.9375, height = 22.0227155455452, class = "dendrogram")
I would like to plot mean CPUE by year and add in CIs that are already calculated.
The CIs are calculated following an approach for trawl survey data so I do not think I can use any of the CI plot functions available in R. I would really appreciate any help.
I have been trying to figure this out following examples I found online, but the CIs are not being plotted on my data points. I have R version 3.1.0 on windows 8. This is my code.
dput(fall)
structure(list(Year = structure(1:7, .Label = c("2007", "2008",
"2009", "2010", "2011", "2012", "2013", "2014"), class = "factor"),
Season = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Fall",
"Spring"), class = "factor"), CPUE = c(2.67597320766895,
1.13720423803133, 3.33880765324431, 0.806172684858967, 1.4489759307485,
10.5492990950043, 4.52479039663784), Variance = c(6.80824504958873,
0.320707030421567, 11.5769406857122, 1.05791053306542, 0.187046436602381,
15.8421823978692, 2.68384838783695), SD = c(2.60926139924476,
0.566310012644635, 3.40249036526369, 1.0285477786984, 0.432488654882855,
3.98022391303168, 1.63824552123207), Number = c(75, 91, 87,
85, 115, 157, 208), CV = c(0.975070076100538, 0.497984437364567,
1.01907348929115, 1.27584052153583, 0.298478839920718, 0.377297475139038,
0.362059980159386), lower = c(2.07563668109912, 1.01926446969017,
2.61363856286983, 0.584320068914576, 1.36908295705723, 9.92183627713643,
4.30084507885747), upper = c(3.27630973423878, 1.2551440063725,
4.06397674361879, 1.02802530080336, 1.52886890443977, 11.1767619128722,
4.7487357144182)), .Names = c("Year", "Season", "CPUE", "Variance",
"SD", "Number", "CV", "lower", "upper"), row.names = c(NA, 7L
), class = "data.frame")
My plot attempt was:
plot(fall$CPUE, type='n', xlab="Year", ylab='Mean CPUE', axes=F)
axis(1, at=1:8, labels=levels(fall$Year))
axis(2)
box()
lines(fall$Year, fall$CPUE, col=1)
points(fall$Year, fall$CPUE, col=1, pch=16)
arrows(y0 = fall$lower, y1 = fall$upper, x0 = fall$CPUE, x1 = fall$CPUE,
length=0.1, code = 3, col = 4, angle = 90)
Andre's answer here helped me to find a solution.
Data for plot
structure(list(CPUE = c(2.67597320766895, 1.13720423803133, 3.33880765324431, 0.806172684858967, 1.4489759307485, 10.5492990950043, 4.52479039663784 ), lower = c(2.25499288520513, 1.04583532734779, 2.80755247046762, 0.640225963050555, 1.37919786502951, 9.90712658332295, 4.26047455308042 ), upper = c(3.09695353013276, 1.22857314871488, 3.870062836021, 0.972119406667378, 1.51875399646749, 11.1914716066857, 4.78910624019525 )), .Names = c("CPUE", "lower", "upper"), class = "data.frame", row.names = c(NA, 7L))
Plot
plot(fall$CPUE, type='l', xlab="Year", ylab='Mean CPUE', axes=F,ylim=c(0,12))
axis(1, at=1:8, labels=levels(fall$Year))
axis(2)
box()
require(plotrix)
plotCI(fall$CPUE,y=NULL,uiw = fall$upper-fall$CPUE,ui=NULL,li=NULL,err="y", sfrac=0.01,gap=0,slty=par("lty"),add=T,scol="black",pch=18,pt.bg=par("bg"))