I am using ezPlot from the ez package in R to plot results of a mixed within and between-ss design. The data point from the two groups I have overlap so that I would like to jitter both the data point and associated error bar.
data<-structure(list(Sub = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("WW", "XX", "YY",
"ZZ"), class = "factor"), DepVar = c(0.67, 0.35, 0.09, 0.2, 0.19,
0.13, 0.45, 0.23, 0.08, 0.32, 0.17, 0.18, 0.67, 0.36, 0.55, 0.4,
0.37, 0.05, 0.26, 0.11, 0.08, 0.46, 0.29, 0.18, 0.16, 0, 0.38,
0.22, 0.08, 0.1, 0.54, 0.17, 0.07, 0.38, 0.75, 0.87, 0.27, 0.57,
0.31, 0.28, 0.07, 0.12, 0.75, 0.33, 0.23, 0.33, 0.26, 0.18),
Group = 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, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor"),
Con = structure(c(1L, 3L, 3L, 3L, 4L, 5L, 2L, 3L, 4L, 1L,
2L, 3L, 1L, 3L, 3L, 3L, 4L, 5L, 2L, 3L, 4L, 1L, 2L, 3L, 1L,
3L, 3L, 3L, 4L, 5L, 2L, 3L, 4L, 1L, 2L, 3L, 1L, 3L, 3L, 3L,
4L, 5L, 2L, 3L, 4L, 1L, 2L, 3L), .Label = c("C", "D", "E",
"F", "G"), class = "factor")), .Names = c("Sub", "DepVar",
"Group", "Con"), class = "data.frame", row.names = c(NA, -48L))
ezPlot( data,
dv = .(DepVar),
wid = .(Sub), # subject
within = .(Con),
between=.(Group),
split=.(Group),
do_bars=TRUE,
type = 2,
x = .(Con))
A non elegant trick is so set scale-color manual white so that the underlying data points disappear and then using geom-point position dodge(0.4))
ezPlot( data,
dv = .(DepVar),
wid = .(Sub), # subject
within = .(Con),
between=.(Group),
split=.(Group),
do_bars=TRUE,
type = 2,
x = .(Con))+
scale_color_manual(values=c("white", "white"))+
geom_point(aes(fill=Group), color="black", pch= 21, size= 3, position=position_dodge(0.4))+
geom_line(aes(group = Group), lty = 3, lwd = 1.3, color='black')
however, I would like to have the error bar plotted and I don't know how to achieve this or if other workarounds are possible. I would like to stick to ezplot. Thanks!
One way is to use set print_code = TRUE, to produce data to be plotted, as well as the ggplot code:
library(ggplot2)
stats <- ezPlot( data,
dv = .(DepVar),
wid = .(Sub), # subject
within = .(Con),
between=.(Group),
split=.(Group),
do_bars=TRUE,
type = 2,
x = .(Con),
print_code = TRUE)
Then, manually modify the code to add position = position_dodge(0.4) to each geom, then run the ggplot code.
A more efficient way to do the same thing would be to capture.output the code as a character vector, use gsub to add position = position_dodge(0.4), then eval(parse(text = ...)) the modified code:
gg_code <- capture.output(stats <- ezPlot( data,
dv = .(DepVar),
wid = .(Sub), # subject
within = .(Con),
between=.(Group),
split=.(Group),
do_bars=TRUE,
type = 2,
x = .(Con),
print_code = TRUE))
gg_code <- gsub("alpha", "position = position_dodge(0.4), alpha", gg_code)
eval(parse(text = paste(gg_code, collapse = "")))
Output:
Related
When I try to perform a meta-analysis with hierarchical weights in robumeta I get
Error in eigen(x) : infinite or missing values in 'x',
using the same data that does not produce any errors with correlational weights.
My data matrix does not have any NA or missing values. Cluster entails whole numbers between 1 and 4.
Does anyone know why I get the Eigen(x) error?
Code needed to reproduce error:
#load data, you need to adjust read.table depending on where the file is saved.
mydata <- read.table ("H:/Desktop/Max_R_Dataset_Meta_Analysis.csv", header = TRUE, sep = ",")
#install & load packages
library (robumeta)
library (devtools)
install_github("jepusto/clubSandwich")
library (clubSandwich)
#fit moderator model with CORR
res_2 <- robu (formula = effect_size ~ pathway, var.eff.size = effect_size_variance, studynum = Study_ID, modelweights = "CORR", rho = 0.8, small = TRUE, data = mydata)
print (res_2)
#fit moderator model with HIER
hier1 <- robu (formula = effect_size ~ pathway, var.eff.size = effect_size_variance, studynum = cluster, modelweights = "HIER", small = TRUE, data = mydata)
print (hier1)
dput (head(mydata,35))
structure(list(Study_ID = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L,
4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 1L, 1L, 1L, 2L,
2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L), effect_size = c(-0.05,
-0.09, -4.44, 0.28, 0.25, 0.91, 0.31, 0.31, 0.33, 0.27, 0.13,
0.71, -0.1, -0.09, -0.28, 0.2, 0.23, 1.23, 0.21, 0.22, 0.29,
-0.18, -0.16, -0.75, 0.2, 0.24, 2.47, 0.37, 0.36, 2.34, 0.17,
0.15, 0.85, 0.04, 0), effect_size_variance = c(0.010737802, 0.008056791,
30.135452, 0.010478163, 0.011260784, 0.093962475, 0.006933061,
0.008891908, 0.007840352, 0.006092875, 0.007411207, 0.040583305,
0.021610499, 0.019590468, 0.104406625, 0.012783255, 0.011467534,
0.333023923, 0.004151044, 0.008464275, 0.006936499, 0.012797742,
0.007904113, 0.307592997, 0.001625522, 0.002084078, 0.230050467,
0.009038613, 0.00895868, 0.34524772, 0.004019923, 0.002854116,
0.078314231, 0.007680706, 0), pathway = c(2L, 4L, 6L, 2L, 4L,
6L, 2L, 4L, 6L, 2L, 4L, 6L, 2L, 4L, 6L, 2L, 4L, 6L, 2L, 4L, 6L,
1L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 5L, 1L, 3L), cluster = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L), Study_Name = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L), .Label = c("Desiree Thesis Arab",
"Desiree Thesis White", "Gijs Direct Replication", "Gijs Indirect Replication",
"Irina Africa Black", "Irina Africa White", "Irina Thesis", "Max Thesis",
"Stein Race", "Yuan Exp1"), class = "factor")), row.names = c(NA,
35L), class = "data.frame")
The HIER version works with the example data provided by the robumeta authors.
Thanks to NelsonGon I got an answer:
My dataset included an effect size of 0 which produces an infinite eigenvalue in the hierarchical model.
That seems to be caused by differences in the calculation of CORR and HIER models:
CORR uses: dframe$weights <- 1 / (dframe$k * dframe$avg.var.eff.size) while HIER uses dframe$weights <- 1 / dframe$var.eff.size. Although both could theoretically produce 0s, you can check it here: github.com/zackfisher/robumeta/blob/master/R/robu.R
The eigen values are actually calculated later in the source code, the Inside Matrix part.
Because HIER divides by var.eff.size, a var.eff.size of 0 produces an error.
I am using facet_grid to plot my data. I have three plots in grid and I Want to fix the ymax (or ylimit) for two of the plots. Currently, I am using following code
f <- ggplot(data=newmel,aes(x=timestamp,y=value,ymin=0,ymax=value))+facet_grid(variable~., scales = "free_y")+
theme(axis.title.x=element_blank(),axis.title.y=element_blank())
f1 <- f + geom_linerange(subset=.(variable=="hpanom")) # require(plyr) for dot function
f2 <- f1 + geom_linerange(subset=.(variable=="lofanom"))
f3 <- f2 + geom_line(subset=.(variable=="power"))
f3
The output plot is:
I want to fix the range of first two (hpanom and lofanom) plots from 0-1 and I do not care for the third one. This is because first two represent probabilites, hence range is always fixed, whereas the third one represent values of which I do not know limits.
Here I am attaching my whole dataset,so that it become easy to replicate the case
structure(list(timestamp = structure(c(1438450200, 1438536600,
1438623000, 1438709400, 1438795800, 1438882200, 1438968600, 1439055000,
1439141400, 1439227800, 1439314200, 1439400600, 1439487000, 1439573400,
1439659800, 1439746200, 1439832600, 1439919000, 1440005400, 1440091800,
1440178200, 1440264600, 1440351000, 1440437400, 1440523800, 1440610200,
1440696600, 1440783000, 1440869400, 1440955800, 1438450200, 1438536600,
1438623000, 1438709400, 1438795800, 1438882200, 1438968600, 1439055000,
1439141400, 1439227800, 1439314200, 1439400600, 1439487000, 1439573400,
1439659800, 1439746200, 1439832600, 1439919000, 1440005400, 1440091800,
1440178200, 1440264600, 1440351000, 1440437400, 1440523800, 1440610200,
1440696600, 1440783000, 1440869400, 1440955800, 1438450200, 1438536600,
1438623000, 1438709400, 1438795800, 1438882200, 1438968600, 1439055000,
1439141400, 1439227800, 1439314200, 1439400600, 1439487000, 1439573400,
1439659800, 1439746200, 1439832600, 1439919000, 1440005400, 1440091800,
1440178200, 1440264600, 1440351000, 1440437400, 1440523800, 1440610200,
1440696600, 1440783000, 1440869400, 1440955800), tzone = "Asia/Kolkata", tclass = c("POSIXct",
"POSIXt"), class = c("POSIXct", "POSIXt")), variable = 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, 1L, 1L, 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, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("power", "hpanom",
"lofanom"), class = "factor"), value = c(713.818544290426, 1.60000010638915e-16,
1588.93456060134, 1080.34496835479, 1602.88616015399, 1325.85664208325,
1487.27242035101, 647.925289288333, 1.60000010638915e-16, 1280.71707200196,
1558.88686231823, 1349.32481298453, 1386.23617766603, 995.841940511863,
961.865845980269, 1153.33076383446, 1601.85509748887, 1346.15843354498,
1209.13461354976, 1060.91957740428, 1963.3986755642, 1995.90120364349,
1.60000010638915e-16, 1391.2765167008, 1198.11633766185, 1202.33001076712,
1508.21685464222, 1299.09592097037, 1.60000010638915e-16, 1.60000010638915e-16,
0.718682639785718, 0.835994256774337, 0.323313580710195, 0.323826587878846,
0.252277897807753, 0.427692277575098, 0, 0.737219412602466, 0.835994256774337,
0.165199776286282, 0.0893388057418736, 0.20964070918484, 0.13145481071022,
0.408292163070401, 0.824540692174425, 0.71239540630304, 0.323313580710195,
0.485828441524218, 0.188258288292337, 0.231165659725455, 0.907512281748878,
0.951462340841186, 0.835994256774337, 0.29824669841755, 0.245223685520087,
0.108813825489535, 0.387339161244294, 0.359556716396615, 0.835994256774337,
0.835994256774337, 0.4, 0.43, 0.13, 0.28, 0.11, 0.1, 0.13, 0.43,
0.43, 0.13, 0.11, 0.01, 0.12, 0.3, 0.6, 0.4, 0.2, 0.11, 0.1,
0.15, 0.67, 1, 0.43, 0.08, 0.08, 0.12, 0.07, 0.08, 0.43, 0.43
)), row.names = c(NA, -90L), .Names = c("timestamp", "variable",
"value"), class = "data.frame")
It's a bit of a hack, but you can plot an "invisible" point by using size=0 at y=1 to force the limit to 1 on the two linerange plots as shown below. I've also removed need to use plyr.
f <- ggplot(data=newmel,aes(x=timestamp,y=value,ymin=0,ymax=value))+facet_grid(variable~., scales = "free_y")
f <- f + theme(axis.title.x=element_blank(),axis.title.y=element_blank())
f <- f + geom_linerange(data=subset(newmel, variable %in% c("hpanom","lofanom")))
# plot invisible point ( size=0) to set upper limit of y axis to 1
f <- f + geom_point(data=subset(newmel, variable %in% c("hpanom","lofanom")),
aes(x=min(timestamp), y=1), size=0)
f <- f + geom_line(data=subset(newmel, variable=="power"))
f
Update for ggplot 2.0
In ggplot 2.0, setting size=0 no longer makes the point invisible. Instead, use colour = NA to make it transparent . New solution is
library(ggplot2)
f <- ggplot(data=newmel,aes(x=timestamp,y=value,ymin=0,ymax=value))+facet_grid(variable~., scales = "free_y")
f <- f + theme(axis.title.x=element_blank(),axis.title.y=element_blank())
f <- f + geom_linerange(data=subset(newmel, variable %in% c("hpanom","lofanom")))
# plot transparent point ( colour = NA) to set upper limit of y axis at 1
f <- f + geom_point(data=subset(newmel, variable %in% c("hpanom","lofanom")),
aes(x=timestamp[1], y=1), colour=NA )
f <- f + geom_line(data=subset(newmel, variable=="power"))
f
I have a transplant experiment for four locations and four substrates (taken from each location). I have determined survival for each population in each location and substrate combination. This experiment was replicated three times.
I have created a lmm as follows:
Survival.model <- lmer(Survival ~ Location + Substrate + Location:Substrate + (1|Replicate), data=Transplant.Survival,, REML = TRUE)
I would like to use the predict command to extract predictions, for example:
Survival.pred <- predict(Survival.model)
Then extract standard errors so that I can plot them with the predictions to generate something like the following plot:
I know how to do this with a standard glm (which is how I created the example plot), but am not sure if I can or should do this with an lmm.
Can I do this or am I as a new user of linear mixed models missing something fundamental?
I did find this post on Stack Overflow which was not helpful.
Based on a comment from RHertel, maybe I should have phrased the
question: How do I plot model estimates and confidence intervals for
my lmer model results so that I can get a similar plot to the one I
have created above?
Sample Data:
Transplant.Survival <- structure(list(Location = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 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), .Label = c("Steninge", "Molle",
"Kampinge", "Kaseberga"), class = "factor"), Substrate = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L,
4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("Steninge",
"Molle", "Kampinge", "Kaseberga"), class = "factor"), Replicate = structure(c(1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 1L, 2L, 3L, 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, 1L, 2L, 3L), .Label = c("1",
"2", "3"), class = "factor"), Survival = c(1, 1, 1, 0.633333333333333,
0.966666666666667, 0.5, 0.3, 0.233333333333333, 0.433333333333333,
0.966666666666667, 0.866666666666667, 0.5, 0.6, 0.266666666666667,
0.733333333333333, 0.6, 0.3, 0.5, 0.3, 0.633333333333333, 0.9,
0.266666666666667, 0.633333333333333, 0.7, 0.633333333333333,
0.833333333333333, 0.9, 0.6, 0.166666666666667, 0.333333333333333,
0.433333333333333, 0.6, 0.9, 0.6, 0.133333333333333, 0.566666666666667,
0.633333333333333, 0.633333333333333, 0.766666666666667, 0.766666666666667,
0.0333333333333333, 0.733333333333333, 0.3, 1.03333333333333,
0.6, 1)), .Names = c("Location", "Substrate", "Replicate", "Survival"
), class = "data.frame", row.names = c(NA, -46L))
Edit: fixed bug in function / figure.
If you like to plot estimates with CI, you may want to look at the sjp.lmer function in the sjPlot package. See some example of the various plot types here.
Furthermore, the arm package provides function for computing standard Errors (arm::se.fixef and arm::se.ranef)
sjp.setTheme("forestgrey") # plot theme
sjp.lmer(Survival.model, type = "fe")
would give following plot
I'm new to R and having a few issues with using ggplot2.
This is an example of my data (subset of larger data set) :
df <-
structure(list(logpvalue = c(22.36, 6.93, 16.78, 1.78, 17.75,
20.99, 21.03, 9.19, 15.01, 22.25, 13.4, 6.47, 1.34, 13.4, 3.21,
0.37, 0.5, 0.12, 1.8, 0.71, 1.15, 6.73, 0.12, 6.97, 0.64, 9.85,
1.45, 1.67, 2.6, 1.8, 1.35, 4.69, 0.37, 1.91, 0.31, 0, 2.45,
1.68, 2.31, 1.35, 6.48, 4.68), SNP = structure(c(1L, 7L, 6L,
5L, 11L, 1L, 9L, 5L, 8L, 11L, 7L, 5L, 8L, 11L, 1L, 7L, 1L, 4L,
2L, 3L, 10L, 7L, 1L, 4L, 2L, 3L, 10L, 4L, 2L, 3L, 10L, 4L, 2L,
3L, 10L, 4L, 2L, 3L, 7L, 9L, 5L, 1L), .Label = c("rs10244", "rs10891244",
"rs10891245", "rs11213821", "rs12296076", "rs138567267", "rs45615536",
"rs6589218", "rs7103178", "rs7127721", "rs7944895"), class = "factor"),
X173 = structure(c(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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("het", "hom"), class = "factor")), .Names = c("logpvalue",
"SNP", "X173"), class = "data.frame", row.names = c(NA, -42L))
I want to plot a boxplot of logpvalue on y axis, with SNP on the x-axis but with each SNP also categorized by whether the patient is het or hom for X173. So from this data I'd imagine 4 boxes on my boxplot.
If possible I'd also like to incorporate the individual data points (dotplot-boxplot overlay) with jitter.
This is the usual code I'd use for a boxplot of logpavlue vs SNP:
qplot(logpvalue, SNP, data = mydata, geom="boxplot")
+ geom_jitter(position=position_jitter(w=0.1, h=0.1)) + theme_bw()
How do I add the extra x variable into this code?
Try this:
boxplot(df$logpvalue~paste(df$SNP,df$X173))
Or using ggolot2 :
library(ggplot2)
ggplot(data=df,aes(SNP,logpvalue,colour=SNP)) +
geom_boxplot() +
geom_jitter() +
facet_grid(.~X173)
I have two questions on building a bar plot by using ggplot().
How to display data format (Sep-12)?
I would like to display the date in the format of Sep-12. My data is a quarterly summary. I would like to show Mar, Jun, Sep and Dec quarters. However, I used the as.Date(YearQuarter) within the ggplot() function. It shows a different sequence of Apr, July, Oct, Jan.
How to increase y axis limit?
The y axis is set at 70%, one of value label is out of the picutre. I have added ylim(0,1) to increase the y limit to 1. However, I lost the percentage format as the y axis is not displaying the percentage anymore.
x4.can.t.m <- structure(list(NR_CAT = 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, 1L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L, 1L, 2L, 3L), .Label = c("0%", "1 to 84%", "85% +"
), class = "factor"), TYPE = structure(c(1L, 1L, 1L, 2L, 2L,
2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("PM BUSINESS", "PM CONSUMER",
"PREPAY"), class = "factor"), YearQuarter = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("2011-09-01",
"2011-12-01", "2012-03-01", "2012-06-01", "2012-09-01"), class = "factor"),
value = c(0.5, 0, 0.5, 0.35, 0, 0.65, 0.28, 0.02, 0.7, 0.4,
0, 0.6, 0.38, 0, 0.62, 0.43, 0.01, 0.56, 0.57, 0, 0.43, 0.35,
0, 0.65, 0.39, 0.01, 0.6, 0.55, 0, 0.45, 0.4, 0.02, 0.58,
0.35, 0.02, 0.63, 0.35, 0, 0.65, 0.55, 0.01, 0.44, 0.47,
0, 0.53)), .Names = c("NR_CAT", "TYPE", "YearQuarter", "value"
), row.names = c(NA, -45L), class = "data.frame")
This is my plot code:
x4.can.t.m$YearQuarter <- as.Date(x4.can.t.m$YearQuarter)
x4.can.t.d.bar <- ggplot(data=x4.can.t.m, aes(x=YearQuarter, y=value,fill=NR_CAT)) +
geom_bar(stat="identity",position = "dodge",ymax=NR_CAT+0.2) +
facet_wrap(~TYPE,ncol=1) +
geom_text(aes(label =paste(round(value*100,0),"%",sep="")),
position=position_dodge(width=0.9),
vjust=-0.25,size=3) +
scale_y_continuous(formatter='percent',ylim=1) +
labs(y="Percentage",x="Year Quarter") +
ylim(0,100%)
x4.can.t.d.bar +scale_fill_manual("Canopy Indicators",values=tourism.cols(c(6,9,8)))+
opts(title="Canopy Indicator: All Customers portout for Network
Issues",size=4)
It looks like you have an older version of ggplot; the following is for ggplot 0.2.9.1. I had to fix several things to make your plot work. Starting from your original definition of x4.can.t.m:
x4.can.t.m$YearQuarter <- format(as.Date(x4.can.t.m$YearQuarter),"%b-%y")
library("scales")
ggplot(data=x4.can.t.m, aes(x=YearQuarter, y=value, fill=NR_CAT)) +
geom_bar(stat="identity", position = "dodge") +
geom_text(aes(label = paste(round(value*100,0),"%",sep=""), group=NR_CAT),
position=position_dodge(width=0.9),
vjust=-0.25, size=3) +
scale_y_continuous("Percentage", labels=percent, limits=c(0,1)) +
labs(x="Year Quarter") +
scale_fill_discrete("Canopy Indicators") +
facet_wrap(~TYPE,ncol=1) +
ggtitle("Canopy Indicator: All Customers portout for Network Issues") +
theme(plot.title = element_text(size=rel(1.2)))
The first part of the question is just achieved by formatting YearQuarter into the format you wanted, leaving it as a string.
The second part specifies the limits in scale_y_continuous and uses the labels argument to specify the formatting function. Note that library("scales") is needed for this part to work.