Thang Khong
2016-08-19 08:03:28 UTC
Dear all,
I want to draw a plot with two different y axes using ggplot2.
df <- read.csv(file.choose(), header = TRUE)
summary(df)
head(df)
##-------------
library(ggplot2)
library(reshape2)
library(reshape)
library(doBy)
library(gmodels)
library(gtable)
library(grid)
#-------
grid.newpage()
#------- Vol Chart
df_wk_sum <- as.data.frame(table(df$WeekNum))
colnames(df_wk_sum) <- c('wk','amount')
df_wk_sum
c1 <- ggplot(df_wk_sum, aes(x = wk,y = amount)) + geom_bar(stat =
'identity', fill='steelblue') +
geom_text(aes(label=amount), vjust = 1.6, color = 'white', size
= 3.5)+
ggtitle("Volume and Delivery LT")
theme_minimal()
c1
###-------
df_avg_lt <- subset(df, select = c("WeekNum", "Delivery.Leadtime"))
df_avg_lt <- summaryBy(data = df_avg_lt, df_avg_lt$Delivery.Leadtime ~
df_avg_lt$WeekNum, FUN = mean, na.rm = TRUE)
df_avg_lt$WeekNum <- as.factor(df_avg_lt$WeekNum)
df_avg_lt
colnames(df_avg_lt) <- c('wk','lt')
df_avg_lt$lt <- round(df_avg_lt$lt, digits = 2)
c2 <- ggplot(df_avg_lt, aes(x = wk, y = lt,group=1)) +
geom_line(color="orange") + geom_point()+
geom_text(aes(label=lt), vjust = 1.3, color='black')
c2
#---------------
g1<-ggplot_gtable(ggplot_build(c1))
g2<-ggplot_gtable(ggplot_build(c2))
pp<-c(subset(g1$layout,name=="panel",se=t:r))
g<-gtable_add_grob(g1,
g2$grobs[[which(g2$layout$name=="panel")]],pp$t,pp$l,pp$b,pp$l)
ia<-which(g2$layout$name=="axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
grid.draw(g)
#-----------------
But it does't work, you can let me know the reasons for this issue, pls!
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I want to draw a plot with two different y axes using ggplot2.
df <- read.csv(file.choose(), header = TRUE)
summary(df)
head(df)
##-------------
library(ggplot2)
library(reshape2)
library(reshape)
library(doBy)
library(gmodels)
library(gtable)
library(grid)
#-------
grid.newpage()
#------- Vol Chart
df_wk_sum <- as.data.frame(table(df$WeekNum))
colnames(df_wk_sum) <- c('wk','amount')
df_wk_sum
c1 <- ggplot(df_wk_sum, aes(x = wk,y = amount)) + geom_bar(stat =
'identity', fill='steelblue') +
geom_text(aes(label=amount), vjust = 1.6, color = 'white', size
= 3.5)+
ggtitle("Volume and Delivery LT")
theme_minimal()
c1
###-------
df_avg_lt <- subset(df, select = c("WeekNum", "Delivery.Leadtime"))
df_avg_lt <- summaryBy(data = df_avg_lt, df_avg_lt$Delivery.Leadtime ~
df_avg_lt$WeekNum, FUN = mean, na.rm = TRUE)
df_avg_lt$WeekNum <- as.factor(df_avg_lt$WeekNum)
df_avg_lt
colnames(df_avg_lt) <- c('wk','lt')
df_avg_lt$lt <- round(df_avg_lt$lt, digits = 2)
c2 <- ggplot(df_avg_lt, aes(x = wk, y = lt,group=1)) +
geom_line(color="orange") + geom_point()+
geom_text(aes(label=lt), vjust = 1.3, color='black')
c2
#---------------
g1<-ggplot_gtable(ggplot_build(c1))
g2<-ggplot_gtable(ggplot_build(c2))
pp<-c(subset(g1$layout,name=="panel",se=t:r))
g<-gtable_add_grob(g1,
g2$grobs[[which(g2$layout$name=="panel")]],pp$t,pp$l,pp$b,pp$l)
ia<-which(g2$layout$name=="axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(1, "npc") + unit(0.15, "cm")
g <- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
g <- gtable_add_grob(g, ax, pp$t, length(g$widths) - 1, pp$b)
grid.draw(g)
#-----------------
But it does't work, you can let me know the reasons for this issue, pls!
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
To post: email ***@googlegroups.com
To unsubscribe: email ggplot2+***@googlegroups.com
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.