我有一个数据框;
![温度] [1]
First row are headers:
LAYER, Number, Share %
Between 100ms - 200ms , 132 , 4.66%
Between 15ms - 30 ms , 475 , 16.8%
Between 200ms - 300ms , 8 , 0.282%
Between 300ms - 400ms , 1 , 0.0353%
Between 30ms - 50 ms , 1329 , 46.9%
Between 400ms - 500ms , 4 , 0.141%
Between 500ms - 600ms , 2 , 0.0706%
Between 50ms - 100ms , 863 , 30.5%
Greater Than 600ms , 1 , 0.0353%
Less Than 15ms , 17 , 0.6%)
我正在绘制它的条形图:
myplot <- ggplot(temp, aes(as.factor(temp$layer), temp$n, label = temp$yuzde)) + xlab("") + ylab("# of SDNNs") +
geom_bar(stat = "identity", width = 0.6, position = "dodge") +
theme(axis.text=element_text(size=8), axis.title = element_text(size=8,face="bold"), axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_x_discrete(limits=c("Less Than 15ms", "Betwenn 15ms - 30 ms", "Betwenn 30ms - 50 ms", "Betwenn 50ms - 100 ms","Betwenn 100ms - 200 ms", "Betwenn 200ms - 300 ms","Betwenn 300ms - 400 ms", "Betwenn 400ms - 500 ms","Betwenn 500ms - 600 ms", "Greater Than 600ms")) +
geom_label()
图表来了;
但是当我排除scale_x_discrete
时,一切正常!
像这样;
[ggplot normal] [3]
但是我想拥有它,因为它是在第一个x轴顺序。
所以,scale_x_discrete
毁了我的情节。我也尝试过因子和级别编辑,但它们也不起作用!
有人可以帮忙解决这个问题吗?
提前致谢。
答案 0 :(得分:0)
最好的方法是重新排序LAYER
中df
因素的级别:
df$LAYER <- factor(df$LAYER, levels = levels(df$LAYER)[c(10,2,5,8,1,3,4,6,7,9)])
然后你可以简单地说:
library(ggplot2)
ggplot(df, aes(x = LAYER, y = Number, label = Share)) +
geom_col() +
geom_label() +
theme(axis.text = element_text(size=8),
axis.title = element_text(size=8,face="bold"),
axis.text.x = element_text(angle = 90, hjust = 1))
我觉得它仍然很难看,但这就是你所问的。
数据:
df <- read.table(text = 'LAYER Number Share
"Between 100ms - 200ms" 132 4.66%
"Between 15ms - 30 ms" 475 16.8%
"Between 200ms - 300ms" 8 0.282%
"Between 300ms - 400ms" 1 0.0353%
"Between 30ms - 50 ms" 1329 46.9%
"Between 400ms - 500ms" 4 0.141%
"Between 500ms - 600ms" 2 0.0706%
"Between 50ms - 100ms" 863 30.5%
"Greater Than 600ms" 1 0.0353%
"Less Than 15ms" 17 0.6% ', header = T)