geom_dotplot scale_x_discrete之间的差距

时间:2016-06-10 13:58:20

标签: r ggplot2

我正在尝试使用geom_dotplot创建一个histodot。出于某种原因,ggplot给了我看起来在我的数据中沿x轴的任意中断。我的数据已经被分类seq(0,22000000,500000),因此我希望在数据中看到实际存在的差距。但是,我很难成功地列出那些breaks()。我不希望看到我的第一次休息,直到$ 6,000,000之后,休息到10,000,000美元。教我的原因是为什么我不能在我的scale_x_discrete上使用labels=scales::dollar

Here是我的数据。

library(ggplot2)
data <- read.csv("data2.csv")

ggplot(data,aes(x=factor(constructioncost),fill=designstage)) + 
  geom_dotplot(binwidth=.8,method="histodot",dotsize=0.75,stackgroups=T) + 
  scale_x_discrete(breaks=seq(0,22000000,500000)) +
  ylim(0,30)

感谢您提供的所有帮助,如果您有任何疑问,请与我们联系!

enter image description here

1 个答案:

答案 0 :(得分:1)

将x轴视为连续而不是因子将为您提供所需的信息。但是,当您尝试处理ggplot2时,您经历了巨大的成本变量范围(0到2,100万)。

因为你的值(0以外)都至少为500000,所以将成本除以100000会使得ggplot2可以处理的比例增加,但也会为你提供所需的可变间距。

注意当我改变数据的比例时,我必须使用binwidth。

ggplot(data, aes(x = constructioncost/100000, fill = signsta)) + 
    geom_dotplot(binwidth = 3, method="histodot", stackgroups=T) + 
    scale_x_continuous(breaks=seq(0, 220, 5))  +
    ylim(0,30)

enter image description here

然后,如果您愿意,可以更改标签以反映整个美元金额。数字太大,您可能需要添加更少或更改方向(或两者)。

ggplot(data, aes(x = constructioncost/100000, fill = signsta)) + 
    geom_dotplot(binwidth = 3, method="histodot", stackgroups=T) + 
    scale_x_continuous(breaks = seq(0, 220, 10), 
                       labels = scales::dollar(seq(0, 22000000, 1000000)))  +
    ylim(0,30) +
    theme(axis.text.x = element_text(angle = 45, hjust = 1))

enter image description here

相关问题