首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用ggplot2在一个图形中绘制两个独立的条形图

使用ggplot2在一个图形中绘制两个独立的条形图
EN

Stack Overflow用户
提问于 2019-06-21 18:08:06
回答 1查看 84关注 0票数 1

我想创建一个简单的气候图,在一个图中绘制两个独立地理位置的降水量和温度。我想使用GGplot来实现这一点。

我的数据集如下:

代码语言:javascript
运行
复制
Climate.Terschelling<- structure(list(Maand = structure(c(5L, 4L, 8L, 1L, 9L, 7L, 6L, 
2L, 12L, 11L, 10L, 3L), .Label = c("april", "augustus", "december", 
"februari", "januari", "juli", "juni", "maart", "mei", "november", 
"oktober", "september"), class = "factor"), Temperatuur_T = c(2.2, 
2.2, 4.1, 4.4, 11, 14.1, 16, 16.3, 14.2, 10.07, 6.5, 3.7), Neerslag_T = c(67L, 
45L, 51L, 43L, 48L, 53L, 73L, 81L, 84L, 84L, 89L, 76L), Temperatuur_NL = c(2.2, 
2.6, 5, 7.6, 12.1, 14.9, 16.7, 16.6, 14.1, 10.5, 6, 3.1), Neerslag_NL = c(69L, 
49L, 60L, 48L, 56L, 68L, 75L, 76L, 74L, 78L, 76L, 78L), maand = structure(1:12, .Label = c("Jan", 
"Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", 
"Nov", "Dec"), class = "factor")), row.names = 2:13, class = "data.frame")

maand <- as.character(c("Jan", "Feb", "Mar", "Apr", "May", 
                                           "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
Climate.Terschelling$maand<-maand
Climate.Terschelling$maand <- factor(Climate.Terschelling$maand, levels=unique(Climate.Terschelling$maand))

我使用此代码在GGplot 2中创建组合图

代码语言:javascript
运行
复制
library(ggplot2)    
ggplot(data = Climate.Terschelling,
             mapping = aes(x = maand, y = Temperatuur_NL, group = 1)) + 
      geom_bar(mapping = aes(y = Neerslag_NL/2), stat = "identity", colour = "black", fill = "dodgerblue1", size =1
               , alpha = 0.3) +
        geom_bar(mapping = aes(y = Neerslag_T/2), stat = "identity", colour = "black", fill = "navyblue", size =1
                 , alpha = 0.3) +
     geom_line(colour = "yellow", size = 1) + 
        geom_line(data = Climate.Terschelling, aes(y=Temperatuur_T), colour = "red", size = 1) + 
      scale_y_continuous(
        "temperatuur, C", 
        sec.axis = sec_axis(~ . * 2, name = "Neerslag, mm"))

我似乎想不出如何绘制两个相邻的降水带,而不是堆叠。优选地,具有限定条的图例。

现在看起来的图:R plot climate graph

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-21 18:50:02

这里的重点是将数据集更新为整洁的格式,以便按适当的变量进行分组。

你不应该有两列"Temperatuur“和两列"Neerslag”与"_T“和"_NL”相结合。如果您设法将该信息放在单独的列中,则操作dataset将会容易得多,并且ggplot代码也会更短:

代码语言:javascript
运行
复制
# reshape dataset
Climate.Terschelling %>%
  gather(type, value, -Maand, -maand) %>%
  separate(type, c("type1", "type2")) -> Climate.Terschelling_upd

 ggplot()+
    geom_col(data = Climate.Terschelling_upd %>% filter(type1 == "Neerslag"), 
             mapping = aes(x = maand, y = value/2, fill=type2), position = "dodge")+
    scale_fill_manual(values=c("dodgerblue1","navyblue"))+
    geom_line(data = Climate.Terschelling_upd %>% filter(type1 == "Temperatuur"), 
              mapping = aes(x = maand, y = value, col = type2, group = type2), size = 1)+
    scale_color_manual(values=c("yellow","red"))+
    scale_y_continuous("temperatuur, C", sec.axis = sec_axis(~ .*2, name = "Neerslag, mm"))

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56701265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档