问题描述
我试图调整ggplot2图中的几个主题元素。我对此多层数字的三个问题/目标是(数据代码如下):$ b $ ol <
将线条/点图例的图例标题更改为SOD-dead Stems。 $ b
我的代码对许多人来说可能是多余的,所以随时随地提供建议,例如只有一个图例。我是ggplot的新手(喜欢它),但迄今为止在音阶方面遇到了麻烦...... b
$ b
我的代码FOR FIGURE:
#####建立数据框:
quag.infect = c(31,52, 58,74,76,85,99,102)
quke.infect = c(10,13,17,20,23,27,28,27)
qusp.hosts =(rep(c (QUAG,QUKE),每个= 8))
year = rep(2004:2011,2)
quag.dead = c(NA,4,11,19,33,38 ,48,49)
quke.dead = c(NA,1,1,2,3,7,8)
my.df = data.frame(Species = qusp .hosts,Year = year,
Inf.ct = c(quag.infect,quke.infect),Sod.Dead = c(quag.dead,quke.dead))
# ####为条形建立灰色:
grays = c(QUAG =gray50,QUKE =gray66)
#####制作多层图形:
library(ggplot2)
plot_x = ggplot(my.df,aes(Year,Inf.ct,fill = Species))+
geom_bar(stat =identity,position =dodge )+
ylab(表达式(茎数(dbh> =1-cm),sep =))+
xlab()+
scale_fill_手册(值=灰色,感染茎,标签= c(黑橡树,海岸活橡树))+
geom_line(aes(y = Sod.Dead,linetype = Species))+
geom_point(aes(y = Sod.Dead,shape = Species))
plot_x
谢谢,
Sarah
。
对于第1点:代替 xlab
,使用 scale_x_continuous
,设置中断
,然后指定一个空标题。
对于第二点:移动<$ c将全局 aes
函数中的$ c> fill = Species 转换为 aes
函数 geom_bar
。
对于第3点:在 scale_shape
和 scale_linetype
函数,确保同一个标题进入这两个函数。
对代码进行这些更改对于ggplot:
pre $ plot_x = ggplot(my.df,aes(Year,Inf.ct))+
geom_bar(aes(fill =物种),stat =identity,position =dodge)+
ylab(表达式(茎的数量(dbh> =1-cm),sep = ))+
scale_x_continuous(,breaks = seq(2004,2011,1))+
scale_fill_manual(values = grey,Infected Stems,labels = c(Black Oak海岸活橡树))+
geom_line(aes(y = Sod.Dead,linetype = Species))+
geom_point(aes(y = Sod.Dead,shape = Species))+
scale_shape(SOD-dead Stems)+
scale_linetype(SOD-dead Stems)
plot_x
给出以下图表:
I am trying to tweak a few thematic elements in a ggplot2 figure. My three questions/goals for this multi-layered figure are (code for data follows at end):
- Create a x-axis tick mark label for all eight year, i.e., 2004-2011. I do not want a 'xlab' though.
- Remove the two filled dots from the "Infected Stems" legend b/c these "geom_bars" should not have dots associated with them (???).
- Change the Legend Title for the lines/points legend to "SOD-dead Stems".
My code is probably redundant to many of you, so feel free to provide suggestions anywhere, such as having only a single legend. I am new to ggplot (like it) but have trouble with "scales" thus far....
MY CODE FOR FIGURE:
##### BUILD UP DATAFRAME:
quag.infect= c(31, 52, 58, 74, 76, 85, 99, 102)
quke.infect= c(10, 13, 17, 20, 23, 27, 28, 27)
qusp.hosts = (rep(c("QUAG", "QUKE"), each=8))
year = rep(2004:2011, 2)
quag.dead = c(NA, 4, 11, 19, 33, 38, 48, 49)
quke.dead = c(NA, 1, 1, 1, 2, 3, 7, 8)
my.df = data.frame(Species=qusp.hosts, Year=year,
Inf.ct = c(quag.infect, quke.infect), Sod.Dead=c(quag.dead, quke.dead))
##### Establish grey colors for bars:
grays = c(QUAG="gray50", QUKE="gray66")
##### Make multi-layered graphic:
library(ggplot2)
plot_x = ggplot(my.df, aes(Year, Inf.ct, fill=Species)) +
geom_bar(stat="identity", position="dodge") +
ylab(expression("Number of stems (dbh">="1-cm)", sep="")) +
xlab("") +
scale_fill_manual(values=grays, "Infected Stems", labels = c("Black Oak", "Coast Live Oak")) +
geom_line(aes(y=Sod.Dead, linetype=Species)) +
geom_point(aes(y=Sod.Dead, shape=Species))
plot_x
Thanks,Sarah
You're not far off.
For point 1: In place of xlab
, use scale_x_continuous
, setting the breaks
, and specifying an empty title.
For point 2: Move fill = Species
out of the global aes
function into an aes
function for geom_bar
.
For point 3: Change the legend title within scale_shape
and scale_linetype
functions, making sure the same title goes into both.
Making these changes to the code for ggplot:
plot_x = ggplot(my.df, aes(Year, Inf.ct)) +
geom_bar(aes(fill=Species), stat="identity", position="dodge") +
ylab(expression("Number of stems (dbh">="1-cm)", sep="")) +
scale_x_continuous("", breaks = seq(2004, 2011, 1)) +
scale_fill_manual(values=grays, "Infected Stems", labels = c("Black Oak", "Coast Live Oak")) +
geom_line(aes(y=Sod.Dead, linetype=Species)) +
geom_point(aes(y=Sod.Dead, shape=Species)) +
scale_shape("SOD-dead Stems") +
scale_linetype("SOD-dead Stems")
plot_x
gives the following plot:
这篇关于ggplot2图中的主题元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!