创建以R为单位的对数比例条形图

创建以R为单位的对数比例条形图,r,bar-chart,R,Bar Chart,我试图创建一个对数刻度的条形图,因为我的数据从3.92到65700不等 这是我迄今为止使用的代码: beach <- c(PlasticsBlue=3.92, PlasticsGrey=65700, FoamsOrange=17.9, FoamsWhite=51300, RopesGreen=9.71, RopesGreen=3140) beach par(mar = c(10, 5, 10, 5)) barplot(beach, names.arg=c("Plastics/B

我试图创建一个对数刻度的条形图,因为我的数据从3.92到65700不等

这是我迄今为止使用的代码:

beach <- c(PlasticsBlue=3.92, PlasticsGrey=65700, FoamsOrange=17.9, FoamsWhite=51300, RopesGreen=9.71, RopesGreen=3140)
beach
par(mar = c(10, 5, 10, 5))

barplot(beach, names.arg=c("Plastics/Blue", "Plastics/Grey", "Foams/Orange", "Foams/White", "Ropes/Green", "Ropes/Green"), col=c("red2", "slateblue4", "red2", "slateblue4", "red2", "slateblue4", "red2"), legend.text = c("Lowest", "Highest"), args.legend=list(cex=0.75,x="topright"), ylim=c(1,100000), log = ("y"), las=2, ylab = expression("mg g"^-1)) 

beach如果你愿意在R中处理细节,你总能得到什么。在这种情况下,绕过R的有用对数轴,构建自己的:

options(scipen=8)
out <- barplot(log10(beach), names.arg=c("Plastics/Blue", "Plastics/Grey", "Foams/Orange",
      "Foams/White", "Ropes/Green", "Ropes/Green"), col=c("red2", "slateblue4", "red2",
      "slateblue4", "red2", "slateblue4", "red2"), legend.text = c("Lowest", "Highest"),
       args.legend=list(cex=0.75,x="topright"), ylim=c(0, 5), las=2, yaxt="n",
       ylab = expression("mg g"^-1))
yval <- c(1, 10, 100, 1000, 10000, 100000)
ypos <- log10(yval)
axis(2, ypos, yval, las=1)
text(out, log10(beach), beach, pos=3, xpd=NA)
选项(scipen=8)

out使用ggplot2和company可能看起来像:

library(dplyr)
library(ggplot2)
library(tibble)
library(scales)

beach <- c(PlasticsBlue = 3.92, PlasticsGrey = 65700, FoamsOrange = 17.9, FoamsWhite = 51300, RopesGreen = 9.71, RopesGreen = 3140) %>%
  enframe() %>%
  mutate(colorID = rep(c('Lowest', 'Highest'), 3))

plot <- beach %>%
  ggplot(aes(x = 1:nrow(beach), y = value, label = value, fill = colorID)) +
  geom_col(stat = 'identity') +
  scale_y_continuous(trans = "log10", labels = label_number(), breaks = c(1, 10, 100, 1000, 10000, 100000)) +
  scale_x_discrete(labels = beach$name, breaks = 1:nrow(beach), limits = 1:nrow(beach)) +
  geom_text(vjust = -1) +
  theme_minimal() +
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line.y = element_line(colour = 'black'),
        legend.position = 'right',
        legend.title = element_blank()) +
  labs(y = expression("mg g"^-1),
       x = 'Category/Sample colour') +
  scale_fill_manual(values = rep(c('slateblue4', 'red2'), 3))
库(dplyr)
图书馆(GG2)
图书馆(tibble)
图书馆(比例尺)
海滩%
enframe()%>%
变异(colorID=rep(c(‘最低’、‘最高’),3))
地块%
ggplot(aes(x=1:nrow(海滩),y=value,label=value,fill=colorID))+
geom_col(stat='identity')+
连续缩放(trans=“log10”,labels=label\u number(),breaks=c(1,10,100,1000,10000,100000))+
比例x离散(标签=海滩$name,间隔=1:nrow(海滩),限制=1:nrow(海滩))+
几何图形文本(vjust=-1)+
主题_极小值()+
主题(panel.background=element_blank(),
panel.grid.major=元素_blank(),
panel.grid.minor=元素_blank(),
y轴=元素线(颜色=黑色),
legend.position='右',
legend.title=元素_blank())+
实验室(y=表达式(“mg g”^-1),
x='类别/样品颜色')+
刻度填充手册(数值=代表(c('slateblue4','red2'),3))
这给了我们:


非常感谢您!我之前试着涉猎GGP的情节,但没有成功,这太棒了。抱歉,这是个错误。你的回答很有用!也非常感谢你!这正是我想做的,但就是从其他帖子中找不出来。非常有用:)