Customize ymax in facet_grid in ggplot2 - r

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

Related

How to decrease the height of the strips while hiding the text in ggplot?

I am trying to change the size of the strips. I want them smaller than what i am getting as they are taking a lot of space. I tried the code in this thread: edit strip size ggplot2. The recommendation is to use theme(strip.text.x = element_text(margin = margin(2,0,2,0, "cm"))). It did not work because i cannot merge it with theme(strip.text = element_blank()). I include the latter line of code because i do not want the name of the countries to show in the strips, i add them manually using geom_text. Is there a way to keep the name of the countries hidden in the strips but make the size of the strips smaller in height?
Here is the code below (the code is long because i add manually a legend to the graph):
library(gtable)
library(gridExtra)
library(ggplot2)
p1= df %>%
ggplot(aes(x= estimate, y=term)) +
geom_text(aes(label = cntry, x = Inf, y = Inf), vjust = 1.75, hjust =1)+
geom_point(mapping=aes(x=estimate, y=term)) +
facet_wrap(vars(cntry,continent))
#Plot it
dummy= df %>%
ggplot(aes(x = estimate, y = term))+
facet_wrap(vars(cntry,continent)) +
geom_rect(aes(fill=continent), xmin=-Inf, xmax=Inf, ymin=-Inf, ymax=Inf) +
theme_minimal() +
theme(strip.text = element_blank())
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(dummy)
gtable_select <- function (x, ...)
{
matches <- c(...)
x$layout <- x$layout[matches, , drop = FALSE]
x$grobs <- x$grobs[matches]
x
}
panels <- grepl(pattern="panel", g2$layout$name)
strips <- grepl(pattern="strip-t", g2$layout$name)
g2$layout$t[panels] <- g2$layout$t[panels] - 1
g2$layout$b[panels] <- g2$layout$b[panels] - 1
new_strips <- gtable_select(g2, panels | strips)
grid.newpage()
grid.draw(new_strips)
gtable_stack <- function(g1, g2){
g1$grobs <- c(g1$grobs, g2$grobs)
g1$layout <- transform(g1$layout, z= z-max(z), name="g2")
g1$layout <- rbind(g1$layout, g2$layout)
g1
}
new_plot <- gtable_stack(g1, new_strips)
grid.newpage()
grid.draw(new_plot)
# Extract only the legend from "dummy" plot
g_legend <- function(dummy){
tmp <- ggplot_gtable(ggplot_build(dummy))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)}
# Assign the legend to a separate object
facet.legend <- g_legend(dummy)
jpeg("legend_graph.jpg", width = 8, height = 6, units = "in", res = 300)
print(grid.arrange(new_plot, facet.legend, nrow = 2, widths = c(7, 1), heights = c(6, 0.01)))
dev.off()
Here is the data:
structure(list(cntry = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("China",
"USA", "Germany", "Canada"), class = "factor"), term = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L), .Label = c("2008", "2009", "2010", "2011", "2012",
"2013", "2014", "2015", "2016", "2017"), class = "factor"), estimate = c(-0.02,
-0.02, -0.05, -0.01, 0, 0.02, -0.01, 0.03, 0.02, 0, 0.09, 0.1,
0.04, 0.13, 0.1, 0.07, 0.08, 0.01, -0.1, -0.09, -0.1, -0.1, -0.13,
-0.12, -0.18, -0.07, -0.04, 0.01, 0, -0.02, -0.02, 0.03, 0.01,
0.01, 0.03, 0.03), continent = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Asia",
"America", "Europe"), class = "factor")), row.names = c(NA, -36L
), class = "data.frame")

R: ddply - Aggregate data by assigning string as variable name

I am given a big data set with several columns. As an example
set.seed(1)
x <- 1:15
y <- letters[1:3][sample(1:3, 15, replace = T)]
z <- letters[10:13][sample(1:3, 15, replace = T)]
r <- letters[20:24][sample(1:3, 15, replace = T)]
df <- data.frame("Number"=x, "Section"=y,"Chapter"=z,"Rating"=r)
dput(df)
structure(list(Number = 1:15, Area = structure(c(1L, 2L, 2L, 3L, 1L, 3L, 3L, 2L, 2L, 1L, 1L, 1L, 3L, 2L, 3L), .Label = c("a", "b", "c"), class = "factor"), Section = structure(c(2L, 3L, 3L, 2L, 3L, 3L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 3L, 2L), .Label = c("j", "k", "l"), class = "factor"), Rating = structure(c(2L, 2L, 2L, 1L, 3L, 3L, 3L, 1L, 3L, 2L, 3L, 2L, 3L, 2L, 2L), .Label = c("A", "B", "C"), class = "factor")), class = "data.frame", row.names = c(NA,-15L))
I would like now to create frequency tables and graphs split by rating and a a chosen category, e.g. via a string:
Category<-"Section"
data_count <- ddply(df, .(get(Category),Rating), 'count')
data_rel_freq <- ddply(data_count, .(Rating), transform, rel_freq = freq/sum(freq))
dput(data_rel_freq)
structure(list(get.Category. = structure(c(2L, 2L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L), .Label = c("j", "k","l"), class = "factor"), Number = c(4L, 8L, 10L, 12L, 1L, 15L, 2L, 3L, 14L, 7L, 9L, 11L, 13L, 5L, 6L), Area = structure(c(3L, 2L, 1L, 1L, 1L, 3L, 2L, 2L, 2L, 3L, 2L, 1L, 3L, 1L, 3L), .Label = c("a", b", "c"), class = "factor"), Section = structure(c(2L, 2L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L), .Label = c("j", "k", "l"), class = "factor"), Rating = structure(c(1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor"), freq = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), rel_freq = c(0.5, 0.5, 0.142857142857143, 0.142857142857143, 0.142857142857143, 0.142857142857143, 0.142857142857143, 0.142857142857143, 0.142857142857143, 0.166666666666667, 0.166666666666667, 0.166666666666667, 0.166666666666667, 0.166666666666667, 0.166666666666667)), class = "data.frame", row.names = c(NA, -15L))
Using ggplot
ggplot(data_rel_freq,aes(x = Rating, y = rel_freq,fill = get(Category)))+
geom_bar(position = "fill",stat = "identity",color="black") +
scale_y_continuous(labels = percent_format())+
labs(x = "Rating", y="Relative Frequency")
The issue is now that "get(Category)" is now treated as a new column
get.Category. Number Area Section Rating freq rel_freq
1 k 4 c k A 1 0.5000000
2 k 8 b k A 1 0.5000000
3 j 10 a j B 1 0.1428571
4 j 12 a j B 1 0.1428571
5 k 1 a k B 1 0.1428571
6 k 15 c k B 1 0.1428571
7 l 2 b l B 1 0.1428571
Moreover, the Number column should be summed, e.g. the other categories (here: Area) should be dropped and it we should have just one line with for Section "k" with Rating "A".
We can use count to get the frequency of the column 'Section' by evaluating the object identifier 'Category' after converting to symbol (sym) and evaluate (!!) it. Within the ggplot syntax, the aes can also take a symbol and can be evaluated as earlier
library(tidyverse)
library(scales)
library(ggplot2)
df %>%
count(!! rlang::sym(Category), Rating) %>%
group_by(Rating) %>%
mutate(rel_freq = n/sum(n)) %>%
ggplot(., aes(x =Rating, y = rel_freq, fill = !! rlang::sym(Category))) +
geom_bar(position = "fill",stat = "identity",color="black") +
scale_y_continuous(labels = percent_format())+
labs(x = "Rating", y="Relative Frequency")
-output

ezPlot overlapping error bar and data

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:

Connecting points on a graph within nested groups of data with ggplot2

I'm having a problem working out how to draw lines between points on a ggplot that are in a nested structure.
What I have is a set of data that is broken down by 3 different nested groups.
Which are then plotted, the first group is used with facet to pair the subgroups (Mutation), the second group then splits the data into the initial experiment (HiSeq) and the replication experiment (MiSeq), while the third group (Grouping) colors and shapes the points by the sample type they are from.
Where I have gotten stuck though is I'd like to link the 2 points (HiSeq/Miseq) within an pair (mutation) via a line to make it easy to workout which two are linked. I've made a mock up which can be seen:
However I'm unable to work out how to do this across the two groups (HiSeq/Miseq) while staying within the top level group (Mutation).
Does any one have a solution to this? A fragment of the data and the code I'm using to build the current graph can be seen below. It may end up being to messy to be presentable but it would be useful to solve.
ggplot(test,aes(y=AR,x=Type,fill=Grouping,colour=Grouping,shape=Grouping)) +
geom_point(binaxis='y',stackdir='center',position=position_dodge(width = 0.2),size=7) +
facet_wrap(~ Mutation,nrow=1) +
xlab("") +
ylab("Allelic Ratio") +
theme_minimal(base_size=20)
example data:
structure(list(Mutation = structure(c(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), .Label = c("chr1:51910329",
"chr1:72951069"), class = "factor"), Type = structure(c(1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L), .Label = c("HiSeq", "MiSeq"), class = "factor"), Grouping = structure(c(3L,
3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L,
2L, 2L, 1L, 3L, 3L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("Offspring (M)", "Offspring (P)", "Proband"
), class = "factor"), Name = c(288458773L, 288458773L, 423125012L,
423125012L, 344991226L, 344991226L, 422977809L, 422977809L, 420753074L,
420753074L, 351142406L, 351142406L, 422743921L, 422743921L, 425596544L,
425596544L, 422595517L, 422595517L, 477342393L, 477342393L, 288458773L,
288458773L, 423125012L, 423125012L, 344991226L, 344991226L, 422977809L,
422977809L, 420753074L, 420753074L, 351142406L, 351142406L, 477342393L,
477342393L, 480773638L, 480773638L), AR = c(0.38, 0.3, 0, 0,
0.375, 0.545, 0.41, 0.388, 0.35, 0.42, 0, 0, NA, 0.59, NA, 0,
0, 0.05, 0, 0, 0.1875, 0.078379734, 0.4, 0.505582473, 0, 0.002394493,
0, 0.002023547, 0, 0.001600569, 0.6, 0.510240797, 0.6, 0.490997813,
0, 0.001785424)), .Names = c("Mutation", "Type", "Grouping",
"Name", "AR"), class = "data.frame", row.names = c(NA, -36L))
I think this may be what you want -- look into geom_line and understanding its group aesthetic:
ggplot(df, aes(x = Type, y = AR, fill = Grouping, color = Grouping, shape = Grouping)) +
geom_point(size = 5) +
geom_line(aes(group = Name)) +
facet_wrap(~ Mutation)

How to display date format (Sep-12) in bar chart and set y axis limit?

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.

Resources