【问题标题】:Plotting values like 1.10e15, 1.3e15, 2e15 using ggplot or spplot [closed]使用 ggplot 或 spplot 绘制 1.10e15、1.3e15、2e15 等值 [关闭]
【发布时间】:2020-01-27 12:37:45
【问题描述】:

我正在绘制数据,范围在 1e15-2e15 之间,不幸的是我无法绘制一个漂亮的折线图。如何使 y 轴缩写为 1e15 而不是 1000000000000000in graph DATA Link

>library(readxl)
>library("xlsx")
>library(ggplot2)
>test <- read.xlsx2("/filepath/test.xlsx", 1, header=TRUE)  
>ggplot(test,aes(x=NO2, y=  SA)) + geom_point()
>class(test$SA)
[1] "factor"
> dput(test)
structure(list(NO2 = structure(1:11, .Label = c("2008", "2009", 
"2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", 
"2018"), class = "factor"), Bangladesh = structure(c(1L, 4L, 
3L, 6L, 7L, 2L, 9L, 5L, 8L, 10L, 11L), .Label = c("1927189454271550", 

> scientific <- function(x) {
    parse(text=gsub("e", " %*% 10^", scales::scientific_format()(x)))
  }
> ggplot(test,aes(x=NO2, y=  SA)) + geom_point() +scale_y_continuous(label=scientific)
Error: Discrete value supplied to continuous scale
> ggplot(test,aes(x=NO2, y=  SA)) + geom_point() +scale_y_continuous(label=scales::scientific_format())
Error: Discrete value supplied to continuous scale

>ggplot(test,aes(x=NO2, y= as.numeric(SA)))+scale_y_continuous(label=scientific_10)

【问题讨论】:

  • 您确定 y 轴变量没有被编码为一个因素吗? class(test$SA) 的剂量是多少?
  • 如果你dput(test)会很有帮助。
  • 使用scale_y_continuous(label=scales::scientific_format())
  • 您的SA 变量应该是数字,所以您应该先尝试ggplot(test,aes(x=NO2, y= as.numeric(SA))),但没有样本数据,这有点像在黑暗中拍摄。

标签: r excel ggplot2 plot


【解决方案1】:
> test$SA=as.numeric(levels(test$SA))[test$SA]
> ggplot(test,aes(x=NO2, y=  SA)) + geom_point() +scale_y_continuous(label=scales::scientific_format())

【讨论】:

    猜你喜欢
    • 2019-11-23
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2023-04-02
    • 2018-07-30
    相关资源
    最近更新 更多