我发现了这个,如何在带有ggplot2的R中将标签放在geom_bar上,但它只是将标签(数字)放在一个条上.
这就是说,每个x轴有两个条,怎么做同样的事情?
我的数据和代码如下所示:
dat <- read.table(text = "sample Types Number
sample1 A 3641
sample2 A 3119
sample1 B 15815
sample2 B 12334
sample1 C 2706
sample2 C 3147", header=TRUE)
library(ggplot2)
bar <- ggplot(data=dat, aes(x=Types, y=Number, fill=sample)) +
geom_bar(position = 'dodge') + geom_text(aes(label=Number))
Run Code Online (Sandbox Code Playgroud)
然后,我们将得到:

似乎数字文本也定位在"闪避"模式中.我搜索了geom_text手册来查找一些信息,但无法使其正常工作.
建议?
我正在努力在我用R中的ggplot2制作的图表中获得正确的变量排序.
假设我有一个数据帧,例如:
set.seed(1234)
my_df<- data.frame(matrix(0,8,4))
names(my_df) <- c("year", "variable", "value", "vartype")
my_df$year <- rep(2006:2007)
my_df$variable <- c(rep("VX",2),rep("VB",2),rep("VZ",2),rep("VD",2))
my_df$value <- runif(8, 5,10)
my_df$vartype<- c(rep("TA",4), rep("TB",4))
Run Code Online (Sandbox Code Playgroud)
产生下表:
year variable value vartype
1 2006 VX 5.568517 TA
2 2007 VX 8.111497 TA
3 2006 VB 8.046374 TA
4 2007 VB 8.116897 TA
5 2006 VZ 9.304577 TB
6 2007 VZ 8.201553 TB
7 2006 VD 5.047479 TB
8 2007 VD 6.162753 TB
Run Code Online (Sandbox Code Playgroud)
有四个变量(VX,VB,VZ和VD),属于两组变量类型(TA和TB).
我想将值绘制为y轴上的水平条,首先按变量组垂直排序,然后按变量名称排序,按年份分面,x轴上的值和填充颜色对应于变量组.(即在这个简化的例子中,顺序应该是,从上到下,VB,VX,VD,VZ)
1)我的第一次尝试是尝试以下方法:
ggplot(my_df,
aes(x=variable, y=value, fill=vartype, order=vartype)) + …Run Code Online (Sandbox Code Playgroud)