I'm having trouble adjusting axis limits in ggplot2 when the variable
corresponding to that axis is a factor. I have attached a minimal
reproducible example in the file demo.txt.
The result (see it by sourcing demo.txt and printing ldCiPlot) is fine
except that I would like the y-axis limits to be 0 and 7 rather than
0.5 and 6.5.
I tried adding xlim(0,7) ("x" because I do a coord_flip()) but this
causes the error
Error: Discrete value supplied to continuous scale
to be thrown. I have Googled around quite a bit and have not managed to
find anything useful that I can understand. I'm sure there is a simple
solution, but I can't find it. Can anyone point me in the right
direction? Ta.
cheers,
Rolf Turner
--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276
#
# Script demo
#
library(ggplot2)
cim <- structure(list(estval = c(95.0598372177329, 65.9807415589531,
65.0869720623762, 53.33770184058, 50.072498670345, 38.409859732603),
lower = c(61.0379367809246, 47.704518120524, 47.7668481532269,
40.888420637935, 39.2569699351463, 31.6204270540546),
upper = c(252.642096309401, 111.943573713694, 106.932728282857,
78.6103902448028, 70.5796504724512, 49.3747630796037),
Trt = structure(6:1, .Label = c("F", "E", "D", "C", "B", "A"),
class = "factor")), row.names = c("A", "C", "B", "D", "E", "F"),
class = "data.frame")
xlim <- c(0,1.05*max(cim[,3]))
mane <- "95% confidence intervals for LD values"
ldCiPlot <- ggplot(cim) +
geom_point(aes(y = estval, x = Trt), shape = 18, size = 3, colour="red") +
geom_errorbar(aes(ymin = lower, ymax = upper, x = as.numeric(Trt)),
width = 0.04*nrow(cim)) +
labs(x = "", y = "TrtTime") +
ylim(xlim[1],xlim[2]) +
theme_bw() +
theme(axis.text = element_text(size = 14), axis.title = element_text(size =
16)) +
theme(plot.title = element_text(hjust = 0.5,face="bold")) +
theme(plot.margin = unit(c(1,1,1,0.5),units="cm")) +
ggtitle(mane) + coord_flip()
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.