gpt4 book ai didi

r - ggplot2 - 缺少一些 x 标签和网格线

转载 作者:行者123 更新时间:2023-12-02 05:39:07 25 4
gpt4 key购买 nike

我正在使用 R 中的 ggplot2 绘制大量数据集。为了使绘图更易于比较,我设置了 y 尺度的限制,但事实证明,“自然顶部”上方没有绘制任何东西的情节。

例如这个情节: Plot with missing labels and lines
(来源:sickel.net)

我已尝试将顶部 y 值设置为 500(并查看顶部的空白区域,我已经设法做到了)但是如何让 ggplot 也绘制 400,450 和 500 的标签以及相应的网格线?

数据在数据帧 df 中,df$dataset 和 df$error 分别作为数据和错误级别。我目前的绘图代码是

max = max(df$dataset + df$error) * 1.05
if(max > 300) max=505

dataplot = ggplot(data=df, aes(x=species,y=dataset,fill=dim,ylim=c(0,max)))
dataplot = dataplot+geom_bar(position=position_dodge())
dataplot = dataplot+scale_fill_manual(values=c("#BBBBBB", "#444444"))
dataplot = dataplot + ylab(ylab) + xlab(xlab) + opts(legend.title=theme_blank())

limits <- aes(ymax = df$dataset+df$error, ymin=df$dataset-df$error)
dodge = position_dodge(width=0.9)

dataplot = dataplot + geom_errorbar(limits, position=dodge, width=0.5)
dataplot = dataplot + opts(panel.background = theme_rect(fill='white', colour='white'))
dataplot = dataplot + opts(panel.grid.major = theme_line(colour = "gray"))

# dataplot = dataplot + coord_cartesian(ylim=c(0, max))
# dataplot = dataplot + scale_y_continuous(limits=c(0, max))

dataplot = dataplot +
opts(axis.text.x = theme_text(colour = "black",angle=90),
axis.text.y = theme_text(colour = "black"))
dataplot = dataplot + opts(legend.key.size = unit(0.3, "cm"))
print(dataplot)

从注释掉的行可以看出,我已经尝试了 coord_cartesian、scale_y_continous 以及现在的 y-lim,但没有任何运气(是的,我知道 coord_cartesian 正在放大)

最佳答案

对于您可以添加的 ylimits:

+ scale_y_continuous(limits=c(0,500))

为了设置刻度点,您可以使用:

breaks=seq(0, 500, 50) # which means ticks from 0-500, every 50 units  

因此,我认为您需要将以下代码添加到您的情节代码中:

+scale_y_continuous(limits=c(0,500),breaks=seq(0, 500, 50))

关于r - ggplot2 - 缺少一些 x 标签和网格线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11273602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com