我有一个包含行的数据框,其中包含期刊出版物的标题,值,并指示它是normal
一个highlight
数据点还是一个数据点。我希望该图保留数据框的顺序。以下代码产生不均匀间隔的y轴。
require(ggplot2)
title <- c("COGNITION","MUTAT RES-DNA REPAIR","AM J PHYSIOL-CELL PH","AM J PHYSIOL-CELL PH","BLOOD",
"PNAS","BIOCHEM BIOPH RES CO","CLIN CANCER RES","BIOCHEM BIOPH RES CO","MOL THER" )
value <- c(-0.428, -0.637, -0.740, -0.782, -0.880, -1.974, -1.988, -2.029, -2.217, -2.242)
indicator <- c(rep("highlight",5), rep("normal",5))
df <- data.frame(title, value, indicator)
mycolors <- c("highlight" = "blue", "normal" = "red")
x_axis_range <- c((min(df$value)), (max(df$value)))
p <- ggplot(df, aes(x = title, y = value)) +
geom_point(aes(size=3, color=indicator)) +
coord_flip() +
scale_color_manual(values=mycolors) +
scale_y_continuous(limit=x_axis_range) +
# produces uneven spacing
scale_x_discrete(limits=df$title) +
theme(legend.position="none")
show(p)
我不知道为什么ggplot是增加的额外的空间MOL THER
和CLIN CANCER RES
与之间BLOOD
和AM J PHYSIOL-CELL PH
数据点。当我将scale_x_discrete()
行更改为:
scale_x_discrete(limits=df$title.1) +
该间隔变得均匀,但是数据的顺序从下到上按标题按字母顺序更改。
为什么要在间距.1
的末尾加上limits=df$title
?如何保持这种均匀性,并且仍然能够使用该order()
功能控制数据在y轴上的顺序?
对于离散刻度,您会得到不均匀的间距,这是因为通过提供df$title
10个值,但是在绘图中只有8个唯一值-因此,已使用的电平有两个空格。
当您提供scale_x_discrete(limits=df$title.1)
限制时,实际上会被忽略,因为title.1
数据中没有列,结果是NULL
要获得订单,您需要提供将其unique()
值df$title
转换为字符的价格(以保持原始顺序)
ggplot(df, aes(x = title, y = value)) +
geom_point(aes(size=3, color=indicator)) +
coord_flip() +
scale_color_manual(values=mycolors) +
scale_y_continuous(limit=x_axis_range) +
scale_x_discrete(limits=unique(as.character(df$title)) )+
theme(legend.position="none")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句