R 对ggplot轴进行排序';根据另一个系数计算的系数';s级

R 对ggplot轴进行排序';根据另一个系数计算的系数';s级,r,ggplot2,R,Ggplot2,我已经成功地创建了一个方框图,显示了一个城市的几个街区的分数,并根据它们所属的地区给它们着色。结果如下所示: # order the data frame as desired df = df[order(df$district, df$neighbourhood), ] # set the neighbourhood levels in the order the occur in the data frame df$neighbourhood = factor(df$neighbourhoo

我已经成功地创建了一个方框图,显示了一个城市的几个街区的分数,并根据它们所属的地区给它们着色。结果如下所示:

# order the data frame as desired
df = df[order(df$district, df$neighbourhood), ]
# set the neighbourhood levels in the order the occur in the data frame
df$neighbourhood = factor(df$neighbourhood, levels = unique(df$neighbourhood))
库(ggplot2)
df=read.csv(“http://pastebin.com/raw/rpPLwSXn")
ggplot(df,aes(x=邻里,y=分数,填充=地区))+
geom_箱线图()+
ggtitle(“邻里得分”)+
实验室(x=“邻里”,y=“分数”,fill=“地区”)+
主题(axis.text.x=元素\文本(角度=45,hjust=1))

它看起来相当不错,尽管事实上不是按照字母顺序(数据框上的
邻里
列)在x轴上对邻里进行排序,我希望它们按照它们所属的Discrct进行排序(
数据框上的district
变量)


我已经读过,我可以使用
因子
来重新调整
邻里
列上的值,但没有成功,因为向量长度不同(比邻里少的地区)

我会沿着面线(~district)在该地区上刻面


请参见?facet_网格和?facet_wrap

我将沿着facet_wrap(~district)线在区域上刻面


参见?刻面网格和?刻面包裹

我喜欢Ulrik答案中的刻面概念-这可能是最好的可视化。要对
邻域
列的因子级别进行排序,最简单的方法可能如下所示:

# order the data frame as desired
df = df[order(df$district, df$neighbourhood), ]
# set the neighbourhood levels in the order the occur in the data frame
df$neighbourhood = factor(df$neighbourhood, levels = unique(df$neighbourhood))

当等级达到你想要的顺序后,坐标轴将随之出现。

我喜欢Ulrik回答中的刻面概念-这可能是最好的可视化。要对
邻域
列的因子级别进行排序,最简单的方法可能如下所示:

# order the data frame as desired
df = df[order(df$district, df$neighbourhood), ]
# set the neighbourhood levels in the order the occur in the data frame
df$neighbourhood = factor(df$neighbourhood, levels = unique(df$neighbourhood))

级别按您想要的顺序排列后,轴将跟随。

因此,对于同一地区的街区,您希望如何排列?首先按字母顺序排列街区,然后按字母顺序排列街区,我会在键盘前立即更新描述。因此,对于同一地区的街区,你想让它们怎么排列?先按字母顺序排列,然后按字母顺序排列,我一看到键盘就会更新描述。你能显示完整的代码吗?我对你的建议的天真解释是,你想添加
+facet\u网格(~district)
,这是一个严重的失败。目前,这似乎更多的是一个评论,而不是一个答案。谢谢你的回答,但镶嵌面不是我想要的。添加
facet\u网格(~district,scales=“free\u x”,space=“free”)
效果很好-一些地区名称不太适合镶嵌面条,但这可以通过换行或其他方式解决。你能展示完整的代码吗?我对你的建议的天真解释是,你想添加
+facet\u网格(~district)
,这是一个严重的失败。目前,这似乎更多的是一个评论,而不是一个答案。谢谢你的回答,但镶嵌面并不是我想要的。添加
facet\u网格(~district,scales=“free\u x”,space=“free”)
效果很好-一些地区名称不太适合镶嵌面条,但可以通过换行或其他方式解决。