[英]Make a bar plot with positive and negative values
You can use the following code您可以使用以下代码
library(ggplot2)
df <- data.frame(names = c("yahoo", "google", "amazon", "aliexpress"), frq = c(3,1,-4,-1))
ggplot(df, aes(x=names, y=frq, fill=names)) +
geom_bar(position=position_dodge(), stat="identity") +
scale_x_discrete(limits = df$names)+ theme(legend.position="none")+
xlab("Names") +
ylab("Frequency")
ggplot(df, aes(x=names, y=frq)) +
geom_bar(aes(fill = frq < 0), stat = "identity") +
scale_fill_manual(guide = FALSE, breaks = c(TRUE, FALSE), values=c("Green", "red")) +
scale_x_discrete(limits = df$names)+ theme(legend.position="none")+
xlab("Names") +
ylab("Frequency")
You can just order the factors according to frequency:您可以根据频率对因素进行排序:
library(ggplot2)
h <- data.frame(names = c("yahoo", "google", "amazon", "aliexpress"), frq = c(3,1,-4,-1))
h$names <- factor(h$names, levels = h$names[order(h$frq)])
h$positive <- ifelse(h$frq>0, 1, 0)
ggplot(h, aes(names, frq, fill=positive)) + geom_bar( stat = "identity")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.