R中theme_bw()
时间: 2024-03-24 19:41:33 浏览: 41
在 R 中,theme_bw() 是 ggplot2 包中的一个函数,用于设置 ggplot2 图形的主题为简洁的黑白主题。使用该函数可以将 ggplot2 图形的背景颜色设置为白色,网格线颜色设置为灰色,坐标轴线颜色设置为黑色等,从而使图形更加清晰易读。
以下是一个示例,使用 theme_bw() 函数将 ggplot2 图形主题设置为黑白主题:
```
library(ggplot2)
ggplot(data = mpg, aes(x = class, y = hwy)) +
geom_boxplot() +
xlab("Vehicle class") +
ylab("Highway miles per gallon") +
ggtitle("Highway MPG by Vehicle Class") +
theme_bw()
```
在这个例子中,我们使用 ggplot2 包绘制了一个箱线图,并使用 theme_bw() 函数将主题设置为黑白主题。这将使图形的背景颜色为白色,网格线颜色为灰色,坐标轴线颜色为黑色。
相关问题
改写;scores(nmds)$sites %>% cbind(df) %>% ggplot(aes(x = NMDS1, y = NMDS2)) + geom_point(aes(size = 0.5, color = Group)) + stat_chull(geom = "polygon", aes(group = Group, color = Group, fill = Group), alpha = 0.1) + annotate("text", x = -0.15, y = 0.1, label = paste0("stress: ", format(nmds$stress, digits = 4)), hjust = 0) + theme_bw(base_size = 18)+ theme(text=element_text(family="A",size=20)),不使用$得到相同结果
可以使用`extract()`函数来从分析结果对象中提取所需的元素。下面是使用`extract()`函数重新编写代码的例子:
```R
nmds_scores <- extract(scores(nmds), "sites")
nmds_df <- cbind(nmds_scores, df)
ggplot(nmds_df, aes(x = NMDS1, y = NMDS2)) +
geom_point(aes(size = 0.5, color = Group)) +
stat_chull(geom = "polygon", aes(group = Group, color = Group, fill = Group), alpha = 0.1) +
annotate("text", x = -0.15, y = 0.1, label = paste0("stress: ", format(nmds$stress, digits = 4)), hjust = 0) +
theme_bw(base_size = 18) +
theme(text = element_text(family = "A", size = 20))
```
这个代码与原始代码的作用是相同的,但使用`extract()`函数来代替了$符号,以便从NMDS分析结果对象中提取所需的元素。
options(repr.plot.width = 10,repr.plot.height = 10) ggplot(data.bar,aes(x = index,y=center,fill=class))+ scale_y_continuous(limits = c(-1,3))=geom_bar(stat = "identity")+ facet_grid(class~.)+guides(fill=FALSE)+theme_bw()+ geom_text(aes(label=round(center,2)),size=4,vjust=-0.5)+ theme(text=element_text(size = 25))Error in `+.gg`(geom_bar(stat = "identity"), facet_grid(class ~ .)) : ℹ Did you forget to add this object to a <ggplot> object?
根据你提供的代码,我观察到你在 ggplot 的语句中,将 `geom_bar` 和 `facet_grid` 直接用 `+` 连接,这里应该将它们放在同一个 `ggplot` 对象中,例如:
```R
options(repr.plot.width = 10,repr.plot.height = 10)
ggplot(data.bar,aes(x = index,y=center,fill=class)) +
geom_bar(stat = "identity") +
facet_grid(class~.) +
guides(fill=FALSE) +
theme_bw() +
geom_text(aes(label=round(center,2)),size=4,vjust=-0.5) +
theme(text=element_text(size = 25))
```
你可以尝试将这个修改后的代码复制到 R 中运行,看看能否解决这个错误。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)