给出如下数据框df
text <- "
CAR_MODEL,ENGINE_VENDOR,var,value,label
Toyota Corolla,FIAT,Three_Family_Pct,33.98,33.98
Nissan Sunny,PRATNEY,Three_Family_Pct,29.84,29.84
Renault Duster,FIAT,Three_Family_Pct,27.86,27.86
Suzuki Ciaz,FIAT,Three_Family_Pct,26.6,26.6
Renault Duster,FIAT,Single_Family_Pct,0,0
Toyota Corolla,FIAT,Single_Family_Pct,0,0
Nissan Sunny,PRATNEY,Single_Family_Pct,0,0
Suzuki Ciaz,FIAT,Single_Family_Pct,0,0
Suzuki Ciaz,FIAT,Two_Family_Pct,42.37,42.37
Renault Duster,FIAT,Two_Family_Pct,41.53,41.53
Toyota Corolla,FIAT,Two_Family_Pct,36.31,36.31
Nissan Sunny,PRATNEY,Two_Family_Pct,32.27,32.27
Nissan Sunny,PRATNEY,Four_Family_Pct,37.89,37.89
Suzuki Ciaz,FIAT,Four_Family_Pct,31.03,31.03
Renault Duster,FIAT,Four_Family_Pct,30.61,30.61
Toyota Corolla,FIAT,Four_Family_Pct,29.71,29.71
Nissan Sunny,PRATNEY,Mileage,12688.5,12688
Suzuki Ciaz,FIAT,Mileage,11989,11989
Renault Duster,FIAT,Mileage,11132.5,11132
Toyota Corolla,FIAT,Mileage,10357,10357
"
df <- read.table(textConnection(text), sep=",", header = T)
我可以将var
绘制成单个图表,方法是var
,如下所示。
library(ggplot2)
ggplot(data=df,
aes(x=CAR_MODEL, y=value, fill=ENGINE_VENDOR, label = label)
) +
geom_bar( stat = "identity", position='dodge') +
geom_text(position = position_dodge(width = 1),
vjust = 1, angle = 0, size = 3 ) +
# facet_grid( var ~ ., scales = "free") +
facet_wrap(~ var, scales = "free_y", ncol=1) +
theme_custom_col +
scale_fill_brewer(palette = "Paired") +
scale_color_brewer(palette = "Paired") +
theme(
text = element_text(size=ggplotAxesLabelSize),
axis.text.x=element_text(angle = 30),
legend.position="top",
axis.text.y=element_blank()
) +
labs( y = "")
我得到一张上面的图表。但是我想在图表中进行两处更改。
答案 0 :(得分:1)
这样的事情?
library(tidyverse)
p1 <- df %>%
mutate(var2=ifelse(var =="Mileage", "Mileage","Family types")) %>%
mutate(var=factor(var, levels = levels(df$var)[c(1,3:5,2)])) %>%
mutate(label2=ifelse(var =="Mileage", scales::comma(label,digits = 0),paste0(label,"%"))) %>%
ggplot(aes(x=CAR_MODEL, y=value, fill=var, label = label2))+
geom_col(aes(col=ENGINE_VENDOR),position = position_dodge(width = 0.9), size=1.2) +
geom_label(position = position_dodge(width = 0.9) ,show.legend = F)+
scale_color_manual(values = c(1,2))+
theme(legend.position="top")
# and the plot using wrap
p1 + facet_wrap( ~ var2, scales = "free_y", ncol = 1)
# and grid
p1 + facet_grid(var2 ~ ENGINE_VENDOR , scales = "free", space = "free_x")
对于网格方法,我会删除aes(col=ENGINE_VENDOR)
参数。
答案 1 :(得分:1)
不是一个完美的解决方案,但会显示所有想要的信息:
library(ggplot2)
ggplot(df, aes(CAR_MODEL, value)) +
geom_rect(aes(xmin = CAR_MODEL, xmax = CAR_MODEL,
ymin = -Inf, ymax = Inf,
color = ENGINE_VENDOR),
size = 60) +
geom_bar(data = subset(df, var != "Mileage"),
aes(fill = var),
stat = "identity", position = "dodge", width = 0.6) +
geom_bar(data = subset(df, var == "Mileage"),
stat = "identity", width = 0.2, fill = "grey20") +
facet_wrap(~ var == "Mileage", ncol = 1, scales = "free") +
labs(x = NULL,
y = NULL,
fill = NULL,
color = NULL) +
scale_fill_brewer(palette = "Accent") +
scale_color_brewer(palette = "Paired") +
theme_classic() +
theme(strip.background = element_blank(),
strip.text.x = element_blank(),
axis.ticks.x = element_blank(),
legend.position = "top") +
guides(colour = guide_legend(override.aes = list(fill = brewer.pal(2, "Paired")[1:2],
size = 1)))
使用facet_wrap
获取里程/非里程信息,使用geom_rect
获取引擎信息。