格式化ggplot2中离散比例的位置 [英] Formatting positions on discrete scale in ggplot2

查看:116
本文介绍了格式化ggplot2中离散比例的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在GGPlot2中创建一个格式良好的2状态凹凸图



在下面的图中,我想减小'white y轴和第一个因子值old之间的空间,并增加第二个值new右侧的空间大小。在真实数据中,我的文本是完整的句子,所以只有第一部分正在显示。



/ kmRAi.pngalt =凹凸图表,左边有太多空格 $ group =old,Rank = 1:5,Text = c(Text1,Text2,Text3,Text4 ,Text5))
new < - data.frame(Group =new,Rank = c(4,2,1,5,3),Text = c(Text1,Text2 ,Text3,Text4,Text5))
df < - rbind(旧,新)

库(ggplot2)

ggplot df,aes(x = Group,y = Rank,group = Text,label = Text))+
geom_line()+
scale_y_reverse()+
geom_text(data = subset(df, Group ==new),size = 3,hjust = 0)


解决方案<您可以在 ggplot()调用中将x变量转换为数字,然后使用 scale_x_continuous()修改轴。

  ggplot(df,aes(x = as.numeric(Gro (),y = Rank,group = Text,label = Text))+ 
geom_line()+
scale_y_reverse()+
geom_text(data = subset(df,Group ==new ),size = 3,hjust = 0)+
scale_x_continuous(limits = c(0.95,5),breaks = c(1,2),labels = levels(df $ Group),
展开= c(0,0))


I'm trying to create a nicely formatted 2-state bump chart in GGPlot2

In the following plot I'd like to reduce the size of the 'white space' between the y axis and the first factor value "old" and increase the size of the space to the right of the second value "new". In the real data my text is full sentences so only the first part is currently being shown.

My code:

old <- data.frame(Group = "old", Rank = 1:5, Text = c("Text1","Text2","Text3","Text4","Text5"))
new <- data.frame(Group = "new", Rank = c(4,2,1,5,3), Text = c("Text1","Text2","Text3","Text4","Text5"))
df <- rbind(old,new)

library(ggplot2)

ggplot(df, aes(x=Group, y= Rank, group =  Text, label = Text)) +
  geom_line() +
  scale_y_reverse() +
  geom_text(data = subset(df, Group == "new"), size=3, hjust=0) 

解决方案

You can convert your x variable to numeric inside the ggplot() call and then use scale_x_continuous() to modify the axis.

ggplot(df, aes(x=as.numeric(Group), y= Rank, group =  Text, label = Text)) +
  geom_line() +
  scale_y_reverse() +
  geom_text(data = subset(df, Group == "new"), size=3, hjust=0) +
  scale_x_continuous(limits=c(0.95,5),breaks=c(1,2),labels=levels(df$Group),
                     expand=c(0,0))

这篇关于格式化ggplot2中离散比例的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆