【问题标题】:Weird conversion of y axis scaley轴刻度的奇怪转换
【发布时间】:2021-03-06 12:28:10
【问题描述】:

我正在尝试绘制时间和不同金属生产的基本区域图。出于某种原因,P1 上的 y 比例正在工作,但 P2 和 P3 上的 y 比例正在改变。我已经完成了我能想到的一切——回到 csv 以检查数据的格式,检查文件加载到 R 时的外观,但似乎没有任何效果。我还有另外 4 个地块,这些地块效果很好,所以我很困惑!剧情是这样出来的:[![image][1]][1]

如果你能帮忙,谢谢

这是我的代码:

library(tidyverse)
theme_set(theme_bw(10))
setwd("/Volumes/GoogleDrive/My Drive/R/Mining/NSW")
copper<-read.csv("copper1.csv",header=TRUE)
lead<-read.csv("lead.csv",header=TRUE)
zinc<-read.csv("zinc.csv",header=TRUE)

p1<-ggplot(copper, aes(x=year, y=tonnes)) +
  geom_area(fill="#7698B8")+
  theme(panel.grid.major = element_blank())+
  theme(panel.grid.minor = element_blank())+
  labs(y = "Copper production (tonnes)", x = "Year")+
  scale_x_continuous(limits=c(1887,2008),breaks=seq(1887,2008,10))+
  theme(axis.ticks = element_blank())+
  theme(axis.title.x = element_blank())+
  theme(axis.text.x = element_blank())
p1

p2<-ggplot(lead, aes(x=year, y=tonnes)) +
  geom_area(fill="#2F67A9")+
  theme(panel.grid.major = element_blank())+
  theme(panel.grid.minor = element_blank())+
  labs(y = "Lead production (tonnes)", x = "Year")+
  scale_x_continuous(limits=c(1887,2008),breaks=seq(1887,2008,10))+
  theme(axis.ticks = element_blank())+
  theme(axis.title.x = element_blank())+
  theme(axis.text.x = element_blank())
p2

p3<-ggplot(zinc, aes(x=year, y=tonnes)) +
  geom_area()+
  theme(panel.grid.major = element_blank())+
  theme(panel.grid.minor = element_blank())+
  labs(y = "Zinc production (tonnes)", x = "Year")+
  scale_x_continuous(limits=c(1887,2008),breaks=seq(1887,2008,10))
p3

library(patchwork)

p1/p2/p3

structure(list(year = 1888:1893, tonnes = c(25L, 33L, 178L, 481L, 
579L, 187L)), row.names = c(NA, 6L), class = "data.frame")

structure(list(year = 1887:1892, tonnes = c(10073L, 18453L, 27704L, 
33380L, 69614L, 61839L)), row.names = c(NA, 6L), class = "data.frame")

structure(list(year = 1897:1902, tonnes = c(10329L, 6198L, 48043L, 
35519L, 37519L, 29903L)), row.names = c(NA, 6L), class = "data.frame")


  


  [1]: https://i.stack.imgur.com/PgoIo.png

【问题讨论】:

  • 嗨,Sophie,你说的尺度变了是什么意思?
  • 您能否解释一下“P2 和 P3 上的 y 比例正在改变”的意思?此外,在这种情节中使用刻面不是更容易吗?
  • 嗨 Stefan 和 Duck,所以它应该是绝对数字的比例,但它正在转换为诸如 2e+05 、 3e+05 等值
  • 如果有帮助,我已经添加了一张照片
  • 添加这个有帮助吗? scale_y_continuous(labels = function(x) format(x, science = FALSE))

标签: r csv ggplot2 area


【解决方案1】:

如果规模是问题,除了 @JordanHackett 的明智建议之外,也可以试试这个:

library(ggplot2)
library(patchwork)
#Plots
p1<-ggplot(copper, aes(x=year, y=tonnes)) +
  geom_area(fill="#7698B8")+
  theme(panel.grid.major = element_blank())+
  theme(panel.grid.minor = element_blank())+
  labs(y = "Copper production (tonnes)", x = "Year")+
  scale_x_continuous(limits=c(1887,2008),breaks=seq(1887,2008,10))+
  scale_y_continuous(labels = scales::comma)+
  theme(axis.ticks = element_blank())+
  theme(axis.title.x = element_blank())+
  theme(axis.text.x = element_blank())
p1

p2<-ggplot(lead, aes(x=year, y=tonnes)) +
  geom_area(fill="#2F67A9")+
  theme(panel.grid.major = element_blank())+
  theme(panel.grid.minor = element_blank())+
  labs(y = "Lead production (tonnes)", x = "Year")+
  scale_x_continuous(limits=c(1887,2008),breaks=seq(1887,2008,10))+
  scale_y_continuous(labels = scales::comma)+
  theme(axis.ticks = element_blank())+
  theme(axis.title.x = element_blank())+
  theme(axis.text.x = element_blank())
p2

p3<-ggplot(zinc, aes(x=year, y=tonnes)) +
  geom_area()+
  scale_y_continuous(labels = scales::comma)+
  theme(panel.grid.major = element_blank())+
  theme(panel.grid.minor = element_blank())+
  labs(y = "Zinc production (tonnes)", x = "Year")+
  scale_x_continuous(limits=c(1887,2008),breaks=seq(1887,2008,10))
p3
#Bind plots
p1/p2/p3

输出:

【讨论】:

  • 感谢大家的帮助!
猜你喜欢
  • 2014-09-29
  • 1970-01-01
  • 2017-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
相关资源
最近更新 更多