R ggplot2中的水平滑动条形图
我已经创建了我需要的绘图,但是我想滑动这些条,这样中间的中性类别对于每个子绘图均匀地跨越零(x=0)。有什么想法吗?也许我没有使用正确的几何构造R ggplot2中的水平滑动条形图,r,ggplot2,R,Ggplot2,我已经创建了我需要的绘图,但是我想滑动这些条,这样中间的中性类别对于每个子绘图均匀地跨越零(x=0)。有什么想法吗?也许我没有使用正确的几何构造 library(ggplot2) survey_data <- data.frame(gender=rep(c("Unreported","Female","Male"),7), feel_job=c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6,
library(ggplot2)
survey_data <- data.frame(gender=rep(c("Unreported","Female","Male"),7),
feel_job=c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7),
Freq=c(0, 0, 0, 1, 3, 5, 0, 4, 4, 0, 7, 15, 3, 28, 35, 3, 35, 80, 1, 52, 108))
p <- ggplot(survey_data, aes(gender)) +
geom_bar(aes(y = Freq, fill = factor(feel_job)), stat = "identity") +
coord_flip()
p
库(ggplot2)
调查数据您可以使用HH
库中的likert
功能进行调查:
library(HH)
survey_data <- data.frame(gender=rep(c("Unreported","Female","Male"),7),
feel_job=c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7),
Freq=c(0, 0, 0, 1, 3, 5, 0, 4, 4, 0, 7, 15, 3, 28, 35, 3, 35, 80, 1, 52, 108))
likert(Freq ~ gender + feel_job, survey_data)
库(HH)
调查数据我猜,因为在因子感觉工作中有7个级别,所以您希望级别=4在y轴上。从在的例子中,我想可能有一种作弊的方法
关键似乎不是依靠ggplot做所有事情。相反,你必须创建你的统计数据,然后摆弄它们。我决定尝试使用geom_rect()
而不是普通的geom_bar()
,这意味着我需要为xmin、xmax、ymin
和ymax
给出每个条的值。这个答案的其余部分将展示如何做到这一点
# save the data we were given
a.survey.data <-survey_data
# going to plot this as rectangles
a.survey.data$xmin[a.survey.data$feel_job < 4] = -a.survey.data$Freq[a.survey.data$feel_job < 4]
a.survey.data$xmin[a.survey.data$feel_job == 4] = -a.survey.data$Freq[a.survey.data$feel_job == 4]/2
a.survey.data$xmin[a.survey.data$feel_job > 4] = 0
a.survey.data$xmax[a.survey.data$feel_job < 4] = 0
a.survey.data$xmax[a.survey.data$feel_job == 4] = a.survey.data$Freq[a.survey.data$feel_job == 4]/2
a.survey.data$xmax[a.survey.data$feel_job > 4] = a.survey.data$Freq[a.survey.data$feel_job > 4]
# assign values to ymin and ymax based on gender and
y.base <- NA
y.base[a.survey.data$gender == "Female"] = 1
y.base[a.survey.data$gender == "Male"] = 2
y.base[a.survey.data$gender == "Unreported"] = 3
a.survey.data$ymin <- y.base + (a.survey.data$feel_job-4)*0.1 - 0.05
a.survey.data$ymax <- y.base + (a.survey.data$feel_job-4)*0.1 + 0.05
# set the labels
a.survey.data$feel_job.cut <- factor(cut(a.survey.data$feel_job,
breaks = c(0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5),
labels = c("1",
"2",
"3",
"neutral",
"5",
"6",
"7"),
ordered = TRUE))
p2 <- ggplot(data = a.survey.data,
aes(xmax = xmax,
xmin = xmin,
ymax = ymax,
ymin = ymin)) +
geom_rect(aes(fill = feel_job.cut)) +
scale_y_continuous(limits = c(0.5,3.5),
breaks=c(1,2,3),
labels=c("Female","Male","Unreported"))
print(p2)
#保存给我们的数据
a、 调查数据4]=0
a、 survey.data$xmax[a.survey.data$feel\u job<4]=0
a、 survey.data$xmax[a.survey.data$feel\u job==4]=a.survey.data$Freq[a.survey.data$feel\u job==4]/2
a、 survey.data$xmax[a.survey.data$feel\u job>4]=a.survey.data$Freq[a.survey.data$feel\u job>4]
#根据性别和性别为ymin和ymax赋值
y、 基地很近。您发布的代码对我的需求不太适用,但看起来该软件包可能会有所帮助。不过,如果可能的话,我想继续使用ggplot2。看看答案,它做了一些非常类似的事情,但在垂直模式下,而不是水平模式下。我认为该图的问题在于它搞乱了条形图中因子的顺序。我也不喜欢堆叠,因为这样你就看不到不同酒吧的实际大小。哎呀,你还在吗?想给我们一些反馈吗?很抱歉耽搁了。文学作品中使用的情节通常是堆叠的,但我认为让它们不堆叠是有价值的。下面提到的HH软件包将完全满足我的要求(如果你能以软件包可以轻松接受的格式获取数据,这是另一项任务),但此时我更愿意使用ggplot2。我想补充一点,即使按照我的标准,这也是一个很糟糕的黑客行为。这与我想要的不完全一样,但已经足够接近了。这是一项强有力的工作。不过,从我的角度来看,你给出的最好建议是不要太依赖ggplot来完成所有事情。