R ggplot条形图额外空间

R ggplot条形图额外空间,r,ggplot2,bar-chart,R,Ggplot2,Bar Chart,我正在尝试使用R中的ggplot绘制图形。以下是我的代码: library(reshape2) library(ggplot2) dfw <- read.table(header=T, text=' Length L1 L2 200:399 27665 28483 400:599 9232 11856 600:799 3347 4072 800:999 1923 2112 1000:1

我正在尝试使用R中的ggplot绘制图形。以下是我的代码:

library(reshape2)
library(ggplot2)
dfw <- read.table(header=T, text='
    Length  L1  L2
    200:399     27665   28483
    400:599     9232    11856
    600:799     3347    4072
    800:999     1923    2112
    1000:1199   1322    1511
    1200:1399   955 1118
    1400:1599   693 850
    1600:1799   496 640
    1800:1999   332 463
    2000:2199   219 306
    2200:2399   142 212
    2400:2599   73  134
    2600:2799   65  121
    2800:2999   39  87
    3000:3199   19  57
    3200:3399   20  31
    3400:3599   14  37
    3600:3799   4   22
    3800:3999   2   17
    4000:9599   8   53
')

 data.long <- melt(data = dfw, id.var = "Length", 
               measure.vars = c("L1", "L2"), variable.name = "Condition")

 ggplot(data=data.long, aes(x=Length, y=value, fill=Condition)) + 
         geom_bar(stat="identity", position=position_dodge(width = 1), 
         colour="black", na.rm = TRUE, width = 0.5) + 
         scale_x_discrete(limits = data.long$Length) + 
         theme(axis.title.x = element_text(face="bold", colour="#990000", size=20),
         axis.text.x  = element_text(angle=90, vjust=2, size=16))
library(重塑2)
图书馆(GG2)

dfw删除此
缩放x_离散(limits=data.long$Length)
并重试,希望这有帮助


编辑:如果您的Y为“缩放Y离散型”(limits=data.long$Length)

则通过删除此选项来删除Y to。代码中的主要问题是变量
长度中的级别顺序错误。
您可以使用函数
factor()
和参数
levels=dfw$Length
修复它(因为原始数据帧的顺序正确)


您已经在这里设置了限制:
scale\u x\u discrete(限制=data.long$Length)
。你试过移除它吗?是的,然后它重新排列了x轴。
data.long$Length<-factor(data.long$Length,levels=dfw$Length)
 ggplot(data=data.long, aes(x=Length, y=value, fill=Condition)) + 
  geom_bar(stat="identity", position=position_dodge(width = 1), colour="black", na.rm = TRUE, width = 0.5) + 
  theme(axis.title.x = element_text(face="bold", colour="#990000", size=20),
        axis.text.x  = element_text(angle=90, vjust=2, size=16))