我将以下数据存储在CSV文件中。
NodeID,pageRank
0,0.0327814931593
1,0.384378430034
2,0.342932804288
3,0.0390870921
4,0.0808856932345
5,0.0390870921
我已经阅读了R中的CSV文件,并按降序对pageRank
值进行了排序。
data <- read.csv("pagerank.csv")
data <- data[order(-data$pageRank),]
订购后,data
紧随其后。
1 0.38437843
2 0.34293280
4 0.08088569
3 0.03908709
5 0.03908709
0 0.03278149
在上面的示例中,第一列表示NodeID
(不顺序),第二列表示pageRank
(降序)。接下来,我使用以下命令来绘制数据。
plot(data$pageRank, type="o", col="red", xlab="Node Rank", ylab="PageRank Value")
该图正确显示了Y轴(pageRank
值)。但是,在X轴上显示的是序号(0,1,2,3,4,5)。因此,除了显示顺序号之外,我如何通过保持{{1} }的降序。我尝试了以下方法。但是,它不会维持NodeID
的降序。
data
答案 0 :(得分:1)
您可以使用forcats
软件包中的为您进行订购。另请参见fct_reorder
。
library(magrittr)
library(tidyverse)
library(forcats)
txt <- "NodeID,pageRank
0,0.0327814931593
1,0.384378430034
2,0.342932804288
3,0.0390870921
4,0.0808856932345
5,0.0390870921"
df <- read_csv(txt)
# Convert NodeID column to factor first
df %<>%
mutate(NodeID = factor(NodeID))
# Plot
ggplot(df, aes(pageRank, fct_reorder(NodeID, pageRank))) +
geom_point() +
geom_segment(aes(y = NodeID, yend = NodeID, x = 0, xend = pageRank), color = "red") +
scale_x_continuous(expand = c(0, 0)) +
ylab("Node Rank") +
xlab("PageRank Value") +
theme_classic(base_size = 16)
ggplot(df, aes(y = pageRank, x = fct_reorder(NodeID, -pageRank))) +
geom_line(group = 1, color = "red") +
scale_y_continuous(expand = c(0, 0)) +
xlab("Node Rank") +
ylab("PageRank Value") +
theme_classic(base_size = 16)
使用基本R图
df$NodeID <- factor(df$NodeID, levels = c("1", "2", "4", "3", "5", "0"))
plot(df$pageRank ~ df$NodeID, xlab = "Node Rank", ylab = "PageRank Value")
由this(v0.2.0.9000)于2018-08-08创建。
答案 1 :(得分:1)