簡體   English   中英

在ggplot2中創建圖,條形圖中的各列並排顯示

[英]Create Plot in ggplot2 where columns in bar chart are side by side

我有一個從1984年到現在的美國車輛燃油經濟性的數據集。 這是我正在使用的數據框的摘要:

   TRANY               avg_city_MPG    avg_highway_MPG
1  Automatic 3-spd     17.52586        21.74484
2  Automatic 4-spd     15.96250        21.72190
3  Automatic 5-spd     15.44277        21.64927
4  Automatic 6-spd     16.67511        23.73770
5   Automatic 6spd     21.00000        34.00000
6  Automatic 7-spd     17.13578        24.47764
7  Automatic 8-spd     16.69271        24.92708
8  Automatic 9-spd     20.39623        28.90566
9     Manual 3-spd     14.25974        16.94805
10    Manual 4-spd     17.41470        21.82131
11    Manual 5 spd     14.00000        14.00000
12    Manual 5-spd     19.29711        25.73959
13    Manual 6-spd     18.17111        26.03095
14    Manual 7-spd     18.07143        25.92857

我最終想要做的是重新創建我在Tableau中制作的繪圖,如下所示: 我要重新創建的Tableau圖的圖像 但是,如果使用ggplot精確地復制它太困難了,那么avg_city_MPG列緊挨avg_highway_MPG列也可以。

這是我到目前為止編寫的繪圖腳本。

  ggplot() + 
  coord_cartesian() + 
  scale_x_discrete() +
  scale_y_continuous() +
  #facet_wrap(~TRANY, ncol=1) +
  labs(title='Average Highway MPG based on transmission ') +
  labs(x=paste("Transmission"), y=paste("Average Highway MPG")) +
  layer(data=bar_chart, 
        mapping=aes(x=TRANY, y=avg_highway_MPG), 
        stat="identity", 
        stat_params=list(), 
        geom="bar",
        geom_params=list(colour="blue"), 
        position=position_dodge()
  ) +
  layer(data=bar_chart, 
        mapping=aes(x=TRANY, y=avg_city_MPG), 
        stat="identity", 
        stat_params=list(), 
        geom="bar",
        geom_params=list(colour="blue"), 
        position=position_dodge()
  )

但是,所有這些都是第一層的條形圖,然后僅顯示第二層的藍線。

謝謝您的幫助!

嘗試這個

dfe <- read.table(header = T, stringsAsFactors = F, text = "  id TRANY TRANY1              avg_city_MPG    avg_highway_MPG
            1  Automatic 3-spd     17.52586        21.74484
           2  Automatic 4-spd     15.96250        21.72190
           3  Automatic 5-spd     15.44277        21.64927
           4  Automatic 6-spd     16.67511        23.73770
           5   Automatic 6spd     21.00000        34.00000
           6  Automatic 7-spd     17.13578        24.47764
           7  Automatic 8-spd     16.69271        24.92708
           8  Automatic 9-spd     20.39623        28.90566
           9     Manual 3-spd     14.25974        16.94805
           10    Manual 4-spd     17.41470        21.82131
           11    Manual 5-spd     14.00000        14.00000
           12    Manual 5-spd     19.29711        25.73959
           13    Manual 6-spd     18.17111        26.03095
           14    Manual 7-spd     18.07143        25.92857")

head(dfe)




dfe <- dfe %>% mutate(TRANY = paste(TRANY, TRANY1, sep = " ")) %>% select(-TRANY1, -id)
library(reshape2)
dfe <- melt(dfe, id.vars = c("TRANY"))


ggplot(aes(x = TRANY, y = value), data = dfe) + geom_bar(stat = "identity") + coord_flip() + facet_wrap(~variable)

在此處輸入圖片說明

比你可以玩的顏色等

暫無
暫無

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

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