R语言 如何将y轴更改为科学注解?字符问题[重复]

j9per5c4  于 2023-06-03  发布在  其他
关注(0)|答案(1)|浏览(71)

此问题已在此处有答案

How can I format axis labels with exponents with ggplot2 and scales?(7个回答)
11小时前关闭
我对ggplot的y轴格式有问题。
在实验中,我们分析了两个站点(A和B)的污染物。我想有科学注解的y轴,但我无法获得它。
这是我做的代码:

library(ggplot)
#create my dataset
a<-c("4000","7500","4000","102000","14500","150000")
b<-c("A","A","A","B","B","B")
data<-data.frame(cbind(a,b))
data$b<-as.factor(data$b)
data$a<-as.integer(data$a)
#plot the data, with scientific annotation
scientific_10 <- function(x) {   parse(text=gsub("e\\+*", " %*% 10^", scales::scientific_format()(x))) }
A<-ggplot(data, aes(x = b, y = a, fill = b)) +
  geom_boxplot() +
  labs(y= "ppm", x = "contaminant")+
  geom_point(shape = 21, position = position_jitterdodge(jitter.width = 0)) 
A<-A+ scale_y_continuous(label=scientific_10)
A

问题是我不知道如何在第一个值处写0。另一个问题是数学表达式中没有写“x”符号。你知道吗?
我附上一张我得到的照片。

非常感谢!

osh3o9ms

osh3o9ms1#

library(ggplot2)
#create my dataset
a<-c("4000","7500","4000","102000","14500","150000")
b<-c("A","A","A","B","B","B")
data<-data.frame(cbind(a,b))
data$b<-as.factor(data$b)
data$a<-as.integer(data$a)
#plot the data, with scientific annotation
scientific_10 <- function(x) {   format(as.numeric(x), scientific = TRUE) }
A<-ggplot(data, aes(x = b, y = a, fill = b)) +
  geom_boxplot() +
  labs(y= "ppm", x = "contaminant")+
  geom_point(shape = 21, position = position_jitterdodge(jitter.width = 0)) + scale_y_continuous(label=scientific_10)
A

相关问题