【问题标题】:Vertical plot visualising positive and negative poles显示正极和负极的垂直图
【发布时间】:2019-12-06 08:57:24
【问题描述】:

我想知道是否可以像 R 中所附的那样创建一个垂直图。

该图没有 X 轴,它旨在可视化由因子分析确定的因子的正负 pols。如果这在 R 中是不可能的,还有哪些其他软件可以创建这样的图(例如,SAS、SPSS)?

一些数据:

bijmb   11.76   
chjoc   8.4    
dejcp   5.03    
memse   13.41    
phjgr   5.86    
altj    7.26    
bujbv   -9.53    
maemj   -10.59    
poejpr  -9.72    
soajs   -10.59    
bijmb   -7.39

【问题讨论】:

  • 您好,欢迎来到 SO !在您的帖子中提供示例数据对于我们进行尝试至关重要。如果您手头没有一些,构建一些可能会很耗时,但我们无法猜测所有内容!
  • 您的“bijmb”类别出现两次 - 这是故意的吗?

标签: r ggplot2


【解决方案1】:

@Tjebo 的答案在图形上更接近您发布的内容,但这种类型的可视化有点难以理解。您必须问自己是否绝对希望它看起来像这样,以牺牲可读性为代价。

我会给你一个用简单的条形图绘制它的替代方法。

dat <- "bijmb   11.76   
chjoc   8.4    
dejcp   5.03    
memse   13.41    
phjgr   5.86    
altj    7.26    
bujbv   -9.53    
maemj   -10.59    
poejpr  -9.72    
soajs   -10.59"
dat <- read.table(text = dat, h = F)
colnames(dat) <- c("name", "value")

library(ggplot2)
library(ggfittext)# for geom_bar_text

ggplot(dat, aes(x = reorder(name, value), y = value, label = name)) + geom_bar(stat = "identity") + coord_flip() + theme_classic() + 
  geom_bar_text(col = "white", place = "left")

【讨论】:

  • 您也可以使用 geom_col ,这与您的 geom_bar(stat = "identity") 基本相同
  • @Tjebo 真的。我只是倾向于_bar,因为我认为“barplot”。
【解决方案2】:

有趣的可视化类型。这是可能的 - 但它有点像黑客。 对于如何处理 geom_text 的灵感,请访问 @gregor 的 answer of this question
这个 hack 的另一个挑战是找到正确的地块尺寸。 R Studio 不会向您显示真正的输出,甚至defining the device size 也没有帮助我。 Or here, a useful cheatsheet for plot sizes 所以我做了我通常做的事情,并在我用ggsave 制作的 jpg 中预览。我的最终情节通常是 pdf。

更多的 cmets 在代码中。

library(ggplot2)

mydf <- read.so::read.so('chjoc   8.4    
dejcp   5.03    
memse   13.41    
phjgr   5.86    
altj    7.26    
bujbv   -9.53    
maemj   -10.59    
poejpr  -9.72    
soajs   -10.59    
bijmb   -7.39', header = FALSE)
#I use the fantastic read.so package for reading data which has been posted not ideally

mydf$value <- 0 # create a value 0 column for the geom_col hack to come
mydf$V2fac <- as.factor(mydf$V2) 
#factorise your values so that your plot is in the right order, 
# and the variable is categorical rather than continuous

p <- ggplot(mydf) + 
  geom_col(aes(x = V2fac, y = 0), color = 'black') + 
  # that's the hack - using color ensures that you will get a line at 0. You can change it's thickness with the size argument 
  geom_text(aes(V2fac, y = 0.2, hjust = 0, label = V1, group = V1), position = position_dodge(0.5)) +
  scale_y_continuous(expand = expand_scale(mult = c(.1, 10))) +
  # here is a lot happening. 
  # You need group for dodging (you have some categories with the same value). 
  # You want the labels aligned - therefore hjust. 
  # You don't want the labels to be cut by the plot borders - therefore the expand argument. 
  # y = 0.2 is basically chosen randomly
  coord_flip() + #that's obviously the key thing to switch all at the end.
  theme(axis.title = element_blank(),
        axis.text.x = element_blank(),
        panel.background = element_blank(),
        panel.grid = element_blank(),
        axis.ticks = element_blank())
# note that using theme after coord_flip is used for the newly assigned x/y axis
# ggsave(plot = p, width = 1.2, height = 5, filename = 'p.jpg')

图片不是reprex代码的输出,只是图片的截图。

reprex package (v0.3.0) 于 2019 年 12 月 6 日创建

【讨论】:

  • 谢谢!是否可以在轴的两个极值点上添加标签?原始图有两个标签:一个在底部,另一个在轴的顶部。
猜你喜欢
  • 2016-05-20
  • 1970-01-01
  • 2020-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多