我对R和SO很陌生,希望您对此有所帮助。
我有factor
17个级别的课程数据。我正在使用library(ggplot2)
和geom_bar(position="fill")
创建比例条形图。代码和输出/情节如下。
基本上,这很好,但我想做的是再创建17个这些图,以突出显示其中一个级别(即一种颜色)保持不变,而其余部分变灰为一种方式区分一个级别与其他级别。因为有17个级别,并且颜色非常相似,所以现在很难区分一些级别。
我希望这是有道理的-乐于编辑和提供更多信息。我会很感激这方面的任何指示或帮助。非常感谢!
# libraries
library(tidyverse) # for the plot
library(ggplot2) # for the plot
library(scales) # for the x-axis scaling
library(lubridate) # for the "POSIXct" and "POSIXt" class
#data classes
class(df.forum$p.date) # "POSIXct" "POSIXt"
class(df.forum$p.forum) # factor
# the plot
df.forum %>%
ggplot(aes(x = p.date, fill = factor(p.forum))) +
geom_bar(position = "fill", stat = "count", show.legend = TRUE) +
theme(axis.text.x = element_text(angle = 90, hjust = 1,vjust = 0.2)) +
scale_x_datetime(date_breaks = "1 month",labels = date_format("%b %Y"), limits = c(mdy_hms("10/1/13 20:00:00"),mdy_hms("5/1/14 20:00:00")))
我也尝试建立一个可重现的示例,似乎这里可能存在问题:
# data
d <- as.POSIXct(
c("2020-01-01", "2020-01-01","2020-01-01",
"2020-01-02", "2020-01-02", "2020-01-02",
"2020-01-03", "2020-01-03", "2020-01-03"))
t <- as.factor(
c("ATopic", "BTopic", "CTopic",
"CTopic", "BTopic", "BTopic",
"CTopic", "ATopic", "BTopic"))
df <- data.frame(d, t)
# the plot
df %>%
ggplot(aes(x = d, fill = factor(t))) +
geom_bar(position = "fill", stat = "count")
##E rror line: position_stack requires non-overlapping x intervals
这将产生以下图形,并显示错误“ position_stack要求不重叠x间隔”:
facet_wrap()
,并gghighlight()
为乡亲。我还尝试了以下代码:
library(gghighlight)
df %>%
ggplot(aes(x = factor(d), fill = factor(t))) +
geom_bar(position = "stack", stat = "count") +
facet_wrap(~t) +
gghighlight()
我认为gghighlight
结合使用facet_wrap
可以满足您的需求。
这是一个使用iris
数据集为每个物种绘制Sepal.Length函数的Sepal.Width的示例。在这里,我通常facet_wrap
将每种物种分开gghighlight
,以便一次只显示一种物种的颜色
library(ggplot2)
library(gghighlight)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species))+
geom_point()+
facet_wrap(~Species)+
gghighlight()
它能回答您的问题吗?
如果没有,请考虑提供数据集的可复制示例(如何制作出色的R可复制示例)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句