以下是我的问题的示例数据:
sampledata <- matrix(c(1:60,1:60,rep(0:1,each=60),sample(1:3,120,replace = T)),ncol=3)
colnames(sampledata) <- c("Time","Gender","Grade")
sampledata <- data.frame(sampledata)
sampledata$Time <- factor(sampledata$Time)
sampledata$Grade <- factor(sampledata$Grade)
sampledata$Gender <- factor(sampledata$Gender)
我正在使用geom_tile
color_palette <- colorRampPalette(c("#31a354","#2c7fb8", "#fcbfb8", "#f03b20"))(length((levels(factor(sampledata$Grade)))))
ggplot(data = sampledata) + geom_tile( aes(x = Time, y = Gender, fill = Grade))+scale_x_discrete(breaks = c("10","20","30","40","50"))+scale_fill_manual(values =color_palette,labels=c("0-1","1-2","2-3","3-4","4-5","5-6",">6"))+ theme_bw()+scale_y_discrete(labels=c("Female","Male"))
我想分别调整男性和女性的宽度,以便为不同的性别获得不同的宽度。宽度的含义如下图所示:
是否可以通过更改当前代码来实现此目的?谢谢!
答案 0 :(得分:1)
如果你想要&#34;宽度&#34;为了相同,您可以在height
中使用geom_tile
美学:
ggplot(data = sampledata) +
geom_tile(aes(x = Time, y = Gender, fill = Grade, height = 0.25))
如果你想让它们独立,那么你需要传递sampledata
data.frame
相同长度的向量,也许最简单的方法是创建一个新变量:
# Assign Females a height of 0.25 and Males a height of 0.75
sampledata$myHeight = ifelse(sampledata$Gender == 0, 0.25, 0.75)
然后:
ggplot(data = sampledata) +
geom_tile(aes(x = Time, y = Gender, fill = Grade, height = myHeight))