我尝试做一个简单的数据框ggplot
structure(list(CLevel = c(3, 4, 5, 6, 7, 8, 9, 10, 11),
Sensitivity = structure(c(1L, 2L, 3L, 3L, 5L, 5L, 7L, 8L, 9L),
.Label = c("56.6666666666667","53.125", "52.9411764705882", "52.9411764705882",
"54.2857142857143", "54.2857142857143", "55.5555555555556", "56.7567567567568",
"57.1428571428571"), class = "factor"),
Specificity = c(76.4705882352941, 76.4705882352941, 76.4705882352941, 76.4705882352941,
76.4705882352941, 76.4705882352941, 76.4705882352941,
76.4705882352941, 76.4705882352941)),
.Names = c("CLevel", "Sensitivity", "Specificity"), row.names = c(NA, -9L),
class ="data.frame")
当我按照以下方式进行绘图时
library(ggplot2)
ggplot() +
geom_point(aes(CLevel, Sensitivity, color = "red",size=12), St) +
geom_point(shape=5)
我得到的x轴没有按照我想要的方式排序。
所以我试过
St$Sensitivity <- factor(St$Sensitivity, levels = St$Sensitivity[order(St$CLevel)])
但我收到了错误
In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels, : duplicated levels in factors are deprecated
所以我再次查看了我的数据框,看起来敏感列中有重复项,因为小数点已经被剥离,所以有些数字完全相同。我想要做的就是订购x轴,这看起来不必要地复杂化。我怎么能这样做?
答案 0 :(得分:1)
如果将“灵敏度”更改为“双精度”,则生成的图表看起来合适:
St <- structure(list(CLevel = c(3, 4, 5, 6, 7, 8, 9, 10, 11),
Sensitivity = c(56.6666666666667, 53.125, 52.9411764705882,
52.9411764705882, 54.2857142857143, 54.2857142857143,
55.5555555555556, 56.7567567567568, 57.1428571428571 ),
Specificity = c(76.4705882352941, 76.4705882352941,
76.4705882352941, 76.4705882352941, 76.4705882352941,
76.4705882352941, 76.4705882352941, 76.4705882352941,
76.4705882352941)),
.Names = c("CLevel", "Sensitivity", "Specificity"),
row.names = c(NA, -9L), class = "data.frame")
library(ggplot2)
ggplot() +
geom_point(aes(CLevel, Sensitivity, color = "red",size=12), St) +
geom_point(shape=5)
答案 1 :(得分:1)
这是你要找的吗?
## to avoid typing "Sensitivity" so many times:
s <- levels(St$Sensitivity)
St2 <- transform(St,Sensitivity=factor(Sensitivity,
levels=s[order(as.numeric(s))]))
library("ggplot2")
ggplot(St2,aes(CLevel,Sensitivity))+
geom_point(color = "red",size=12, shape=5)
请注意,我将映射(aes()
)规范中的颜色,大小和形状规范放在之外,我猜这是你真正想要的......
正如@VeerendraGadekar所说,警告的出现是因为你给我们的价值确实在因子水平上有重复。特别是,直接引用您给我们的结构(为了清晰起见,只是稍微重新排列间距)
.Label = c("56.6666666666667",
"53.125",
"52.9411764705882",
"52.9411764705882", ## duplicate
"54.2857142857143",
"54.2857142857143", ## duplicate
"55.5555555555556",
"56.7567567567568",
"57.1428571428571")
也许你在上游某处失去了精确度?
答案 2 :(得分:0)
<强>编辑:强>
我看到级别为字符,以避免在使用数字时使用""
这应该是适当的样本数据
St = structure(list(CLevel = c(3, 4, 5, 6, 7, 8, 9, 10, 11), Sensitivity = structure(c(1L, 2L, 3L, 3L, 5L, 5L, 7L, 8L, 9L), .Label = c(56.6666666666667,53.125, 52.9411764705882, 52.9411764705882, 54.2857142857143, 54.2857142857143, 55.5555555555556, 56.7567567567568, 57.1428571428571), class = "factor"), Specificity = c(76.4705882352941, 76.4705882352941, 76.4705882352941, 76.4705882352941, 76.4705882352941, 76.4705882352941, 76.4705882352941, 76.4705882352941, 76.4705882352941)), .Names = c("CLevel", "Sensitivity", "Specificity"), row.names = c(NA, -9L), class ="data.frame")
您可以尝试这样订购
ggplot() +
geom_point(aes(CLevel, reorder(Sensitivity, -as.vector(Sensitivity)), color = "red",size=12), St) +
geom_point(shape=5)
# or use this: reorder(Sensitivity, as.vector(Sensitivity)) based on your requirement
需要