簡體   English   中英

有沒有一種方法可以使用barplot或ggplot在同一張圖上同時顯示一個barplot和一個堆疊barplot?

[英]Is there a way to have a barplot and a stacked barplot on the same graph using barplot or ggplot?

我有兩個要疊加到同一圖上的數據。 我看過幾本ggplot文章,但我認為ggplot中不可能。 所以我一直在使用barplot。 我有5層,我正在按層繪制總金額,以實線表示。

然后,我還有另一個數據,代表兩種不同類型的工作人員在這些層中的任務數量。 我將其作為堆積的條形圖。 但是我想在同一張圖上顯示它們,其中總美元金額為一個小節,然后在其旁邊顯示對應的堆疊小節。

這是情節: 按工作人員類型划分的全部任務的堆疊條形圖

每層總美元條形圖

第一張圖的數據如下所示(它是一個表):

        1     2     3     4     5
  0     9   340    97   812  4271
  1     1   417   156  3163 11314

第二張圖的數據如下所示(這是一個數據集):

    Tier    variable    value
1   1   Opp_Amt 16200.00
2   2   Opp_Amt 116067.50
3   3   Opp_Amt 35284.12
4   4   Opp_Amt 278107.10
5   5   Opp_Amt 694820.29

我想將圖放在彼此的頂部,但條形圖保持重疊,並且希望它們並排顯示。

到目前為止的代碼。

par(mar=c(2.5, 4, 4, 4)+2)
## Plot first set of data and draw its axis
barplot(data1$value, axes=FALSE,ylim=c(0,700000), xlab="", ylab="", 
        col="black",space=-10,main="Work Score")
axis(2, ylim=c(0,700000),col="black",las=1)  ## las=1 makes horizontal labels
mtext("Total Opportunity Amount",side=2,line=3.5)
box()

## Allow a second plot on the same graph
par(new=TRUE)

## Plot the second plot and put axis scale on right


m <- barplot(counts,  xlab="", ylab="", ylim=c(0,16000),axes=FALSE, col=c("red","darkblue"),space=3,width=0.5,density=20)
## a little farther out (line=4) to make room for labels
mtext("Task Ratio: Outbound to AE",side=4,col="red",line=3.5) 
axis(4, ylim=c(0,16000), col="red",col.axis="black",las=1)

這給了我 疊加圖

看來您正在嘗試在一張圖表上繪制兩個不同y比例的兩個變量。 我建議您反對這樣做,這被認為是不好的做法。 例如,請參見@hadley的(ggplot2的作者)在此處回答類似問題: https ://stackoverflow.com/a/3101876/3022126

如果兩個變量具有可比的比例,則可以在一個 y軸上繪制兩個變量,但是兩個數據集的范圍不會很大地重疊。

考慮其他可視化效果,也許使用兩個單獨的圖表。

使用ggplot ,我會做類似的事情之一。 他們分別繪制了兩組數據。 首先將數據排列到一個數據幀中,然后使用facet_wrap()並排放置繪圖。 第二種方法分別生成兩個圖對象,然后將兩個圖和圖例組合成一個組合圖。

但是,如果您確實需要“雙y軸”方法,則可以進行一些擺弄,並使用繪圖的布局和gtable函數,可以做到這一點( 使用從此處借來的代碼 )。

像這樣:

library(ggplot2)
library(gtable)
library(plyr)

df1 <- data.frame(Tier = rep(1:5, each = 2),
       y = c(9, 1, 340, 417, 97, 156, 812, 3063, 4271, 11314),
       gp = rep(0:1, 5))

df2 <- read.table(text = "
    Tier    variable    value
   1   Opp_Amt 16200.00
   2   Opp_Amt 116067.50
   3   Opp_Amt 35284.12
   4   Opp_Amt 278107.10
   5   Opp_Amt 694820.29", header = TRUE)


dfA = df1
dfB = df2
names(dfA) = c("Tier", "Value", "gp")
dfA$var = "Task Ratio"
dfB = dfB[,c(1,3)]
dfB$gp = 3
dfB$var = "Total Opportunity Amount"
names(dfB) = names(dfA)
df = rbind(dfA, dfB)
df$var = factor(df$var)
df$var = factor(df$var, levels = rev(levels(df$var)))

 ggplot(df, aes(Tier, Value, fill = factor(gp))) +
    geom_bar(position = "stack", stat = "identity") +
    facet_wrap( ~ var, scale = "free_y") +
    scale_fill_manual("Group", breaks = c("0","1"), values = c("#F8766D", "#00BFC4", "black")) +
    theme_bw() +
    theme(panel.spacing = unit(2, "lines"),
          panel.grid = element_blank()) 



或這個:

p1 <- ggplot(df1, aes(factor(Tier), y, fill = factor(gp))) +
   geom_bar(position = "stack", stat = "identity") +
   #guides(fill = FALSE) +
   scale_y_continuous("Task Ratio",
     limit = c(0, 1.1*max(ddply(df1, .(Tier), summarise, sum = sum(y)))), 
     expand = c(0,0)) +
   scale_x_discrete("Tier") +
   theme_bw() +
   theme(panel.grid = element_blank())

p2 <- ggplot(df2, aes(factor(Tier), value)) +
   geom_bar(stat = "identity") +
   scale_y_continuous("Total Opportunity Amount", limit = c(0, 1.1*max(df2$value)),  expand = c(0,0)) +
   scale_x_discrete("Tier") +
   theme_bw() +
   theme(panel.grid = element_blank())    

# Get the ggplot grobs,
# And get the legend from p1
g1 <- ggplotGrob(p1)
leg = gtable_filter(g1, "guide-box")
legColumn = g1$layout[which(g1$layout$name == "guide-box"), "l"]
g1 = g1[,-legColumn]
g2 <- ggplotGrob(p2)

# Make sure the width are the same in g1 and g2
library(grid)
maxWidth = unit.pmax(g1$widths, g2$widths)

g1$widths = as.list(maxWidth)
g2$widths = as.list(maxWidth)

# Combine g1, g2 and the legend
library(gridExtra)
grid.arrange(arrangeGrob(g2, g1, nrow = 1), leg,
   widths = unit.c(unit(1, "npc") - leg$width, leg$width), nrow=1)



或雙Y軸方法 (但由於@Phil的帖子中給出的原因而不建議使用):

width1 = 0.6       # width of bars in p1
width2 = 0.2       # width of bars in p2
pos = .5*width1 + .5*width2    # positioning bars in p2

p1 <- ggplot(df1, aes(factor(Tier), y, fill = factor(gp))) +
   geom_bar(position = "stack", stat = "identity", width = width1) +
   guides(fill = FALSE) + 
   scale_y_continuous("", 
     limit = c(0, 1.1*max(ddply(df1, .(Tier), summarise, sum = sum(y)))), 
     expand = c(0,0)) +
   scale_x_discrete("Tier") +
   theme_bw() +
   theme(panel.grid = element_blank(),
         axis.text.y = element_text(colour = "red", hjust = 0, margin = margin(l = 2, unit = "pt")),
         axis.ticks.y = element_line(colour = "red"))

p2 <- ggplot(df2, aes(factor(Tier), value)) +
   geom_blank() +
   geom_bar(aes(x = Tier - pos), stat = "identity", width = width2) +
   scale_y_continuous("", limit = c(0, 1.1*max(df2$value)),  expand = c(0,0)) +
   theme_bw() +
   theme(panel.grid = element_blank())

# Get ggplot grobs
g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)

# Get locations of the panels in g1
pp1 <- c(subset(g1$layout, name == "panel", se = t:r))

## Get bars from g2 and insert them into the panel in g1
g <- gtable_add_grob(g1, g2$grobs[[which(g2$layout$name == "panel")]][[4]][[4]], pp1$t, pp1$l)

# Grab axis from g1, reverse elements, and put it on the right
index <- which(g1$layout$name == "axis-l")
grob <- g1$grobs[[index]]
axis <- grob$children[[2]]
axis$widths <- rev(axis$widths)
axis$grobs <- rev(axis$grobs)
axis$grobs[[1]]$x <- axis$grobs[[1]]$x - unit(1, "npc") + unit(3, "pt")

g <- gtable_add_cols(g, g1$widths[g1$layout[index, ]$l], pp1$r)
g <- gtable_add_grob(g, axis, pp1$t, pp1$l+1)

# Grab axis from g2, and put it on the left
index <- which(g2$layout$name == "axis-l")
grob <- g2$grobs[[index]]
axis <- grob$children[[2]]
g <- gtable_add_grob(g, rectGrob(gp = gpar(col = NA, fill = "white")), pp1$t-1, pp1$l-1, pp1$b+1)
g <- gtable_add_grob(g, axis, pp1$t, pp1$l-1)

# Add axis titles
# right axis title
RightAxisText = textGrob("Task Ratio", rot = 90, gp = gpar(col = "red"))
g <- gtable_add_cols(g, unit.c(unit(1, "grobwidth", RightAxisText) + unit(1, "line")), 5)
g <- gtable_add_grob(g, RightAxisText, pp1$t, pp1$r+2)

# left axis title
LeftAxisText = textGrob("Total Opportunity Amount", rot = 90)
g <- gtable_add_grob(g, LeftAxisText, pp1$t, pp1$l-2)
g$widths[2] <- unit.c(unit(1, "grobwidth", LeftAxisText) + unit(1, "line"))

# Draw it
grid.newpage()
grid.draw(g)

在此處輸入圖片說明

嘗試查看barplotadd參數。

## Function to create alpha colors for illustration.
col2alpha <- function(col, alpha = 0.5) {
  tmp <- col2rgb(col)
  rgb(tmp[1]/255, tmp[2]/255, tmp[3]/255, alpha)  
}

## Some fake data
dat1 <- data.frame(id = 1:4, val = c(10, 8, 6, 4))
dat2 <- data.frame(id = 1:4, val = c(4, 6, 8, 10))

barplot(dat1$val, col = col2alpha("blue"))
barplot(dat2$val, col = col2alpha("red"), add = TRUE)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM