R 将空条形图添加到(百分比)条形图(由来自AT的长数据生成)
我想在条形图中将特定项目作为空条添加到x轴 例如:R 将空条形图添加到(百分比)条形图(由来自AT的长数据生成),r,ggplot2,R,Ggplot2,我想在条形图中将特定项目作为空条添加到x轴 例如: # load packages library(reshape2) library(tidyverse) library(scales) #get data data(tips, package = "reshape2") tips # make plot myplot <- ggplot(tips, aes(day, group = sex)) + geom_bar(aes(y = ..prop.., fill = fac
# load packages
library(reshape2)
library(tidyverse)
library(scales)
#get data
data(tips, package = "reshape2")
tips
# make plot
myplot <- ggplot(tips, aes(day, group = sex)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
scale_y_continuous(labels=scales::percent) +
ylab("relative frequencies") +
facet_grid(~sex)
myplot
#加载包
图书馆(E2)
图书馆(tidyverse)
图书馆(比例尺)
#获取数据
数据(提示,package=“重塑2”)
提示
#密谋
myplot以下代码将首先获取所有工作日,然后在不删除未使用级别的情况下绘制图形
tips$day <- as.character(tips$day)
days_levels <- weekdays(Sys.Date() + 1:7, abbreviate = TRUE)
fri <- grep("Fri", days_levels)
days_levels <- c(days_levels[fri:length(days_levels)], days_levels[1:(fri - 1)])
days_levels[days_levels == "Thu"] <- "Thur"
tips$day <- factor(tips$day, levels = days_levels)
# make plot
myplot <- ggplot(tips, aes(day, group = sex)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
scale_y_continuous(labels=scales::percent) +
scale_x_discrete(drop = FALSE) +
ylab("relative frequencies") +
facet_grid(~sex)
myplot
tips$day您只需将天数添加到您的因子级别:
tips$day <- factor(tips$day, levels = c(attributes(tips$day)$levels, "Tues", "Wed"))
完整代码
# load packages
library(reshape2)
library(tidyverse)
library(scales)
# get data
data(tips, package = "reshape2")
tips
tips$day <- factor(tips$day, levels = c(attributes(tips$day)$levels, "Tues", "Wed"))
# make plot
myplot <- ggplot(tips, aes(day, group = sex)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
scale_y_continuous(labels=scales::percent) +
ylab("relative frequencies") +
scale_x_discrete(drop = FALSE) +
facet_grid(~sex)
myplot
#加载包
图书馆(E2)
图书馆(tidyverse)
图书馆(比例尺)
#获取数据
数据(提示,package=“重塑2”)
提示
小费$day你希望酒吧周五开始吗?这能回答你的问题吗@是的,那很好。谢谢。@Tjebo不完全,我不知道我可以简单地添加因子来生成空条,你提到的问题只是解释如何保存它们,而不是如何生成它们。
scale_x_discrete(drop = FALSE)
# load packages
library(reshape2)
library(tidyverse)
library(scales)
# get data
data(tips, package = "reshape2")
tips
tips$day <- factor(tips$day, levels = c(attributes(tips$day)$levels, "Tues", "Wed"))
# make plot
myplot <- ggplot(tips, aes(day, group = sex)) +
geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
scale_y_continuous(labels=scales::percent) +
ylab("relative frequencies") +
scale_x_discrete(drop = FALSE) +
facet_grid(~sex)
myplot