目标是在y轴上实现有序的类别。
$ipDefault = (Get-NetIPAddress -InterfaceAlias "Ethernet" -AddressFamily IPv4).IPAddress
$threeOctets = $ipDefault.Substring(0,$ipDefault.LastIndexOf('.'))
$newIP = $threeOctets + ".105"
。
这里是例子:
y1 -> y2 -> y3
这似乎是相反的。这是在require(data.table)
require(ggplot2)
dt <- data.table(var = rep("x1", 3),
categ = paste0("y", c(1,2,3)),
value = c(-2,0.5,-1))
ggplot(dt, aes(x = categ, y = value)) +
geom_bar(stat = "identity") +
coord_flip() +
theme_bw()
中实现所需排序的一种方法:
ggplot2
太好了,现在订购似乎是正确的。但是进行了一些修改:
dt$categ <- factor(dt$categ, levels = rev(levels(factor(dt$categ))))
ggplot(dt, aes(x = categ, y = value)) +
geom_bar(stat = "identity") +
coord_flip() +
theme_bw()
出于某种原因,此处忽略因子排序。有什么线索吗?
答案 0 :(得分:0)
解决方案可能是:
# dt is original data without factors
ggplot(dt, aes(categ, value, fill = value >= 0)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("darkred", "darkblue")) +
# since we want y1 on top and y3 on bottom we have to apply rev
scale_x_discrete(limits = rev(dt$categ)) +
coord_flip() +
theme_bw()
技巧是将dt$categ
作为limits
的参数传递给scale_x_discrete()
。在您的第一个打印顺序不可反转中,这就是应该这样的方式:ggplot2
从轴的原点(0
)开始放置值。
我还删除了以 not-ggplot 方式使用的两条geom_bar
行。