Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2:theme(legend.title)忽略vjust和hjust参数_R_Ggplot2 - Fatal编程技术网

R ggplot2:theme(legend.title)忽略vjust和hjust参数

R ggplot2:theme(legend.title)忽略vjust和hjust参数,r,ggplot2,R,Ggplot2,从下面的代码中可以看出,主题(legend.title)中的参数hjust和vjust没有任何作用(至少在我的机器上是这样)。两个ggplot()调用生成的两个绘图相同。我测试了theme\u bw()是否是罪魁祸首,但事实并非如此。移动图例标题的解决方案是什么 library(ggplot2) library(reshape) library(RColorBrewer) test <- structure(list(names = structure(c(1L, 4L, 5L, 6L,

从下面的代码中可以看出,
主题(legend.title)
中的参数
hjust
vjust
没有任何作用(至少在我的机器上是这样)。两个
ggplot()
调用生成的两个绘图相同。我测试了
theme\u bw()
是否是罪魁祸首,但事实并非如此。移动图例标题的解决方案是什么

library(ggplot2)
library(reshape)
library(RColorBrewer)
test <- structure(list(names = structure(c(1L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 2L, 3L), .Label = c("Spec1", "Spec10", "Spec11", "Spec2", 
"Spec3", "Spec4", "Spec5", "Spec6", "Spec7", "Spec8", "Spec9"
), class = "factor"), A = c(0.217031528, 0.370637045, 0.152473461, 
0.08340091, 0.094257483, 0.00619633, 0.043205056, 0.017717884, 
0.004354587, 0.000349229, 0.010376486), B = c(0.312070285, 0.311236549, 
0.139193608, 0.07284637, 0.097903335, 0.003568091, 0.028477238, 
0.021042976, 0.004963161, 0.000374577, 0.00832381), C = c(0.281876963, 
0.326092831, 0.152957419, 0.07237009, 0.10259602, 0.004158686, 
0.026662316, 0.020823785, 0.004313649, 0.00037274, 0.007775501
), D = c(0.275453548, 0.337083347, 0.154469202, 0.070573145, 
0.099172727, 0.005437018, 0.02768352, 0.018713749, 0.003950163, 
0.000362092, 0.00710149), E = c(0.278508956, 0.334527205, 0.154663425, 
0.068355587, 0.101934925, 0.005645608, 0.025961027, 0.019175166, 
0.004090645, 0.000386265, 0.00675119), F = c(0.306981913, 0.328928827, 
0.147765154, 0.058429727, 0.094863912, 0.003795248, 0.024415702, 
0.02349575, 0.003980418, 0.000353114, 0.006990233)), .Names = c("names", 
"A", "B", "C", "D", "E", "F"), class = "data.frame", row.names = c(NA, 
-11L))

n1 <- as.vector(test$names)
test.melt <- melt(test, id.vars="names")
names(test.melt) <- c("Species", "Treatment", "Abundance")

colourCount <- length(unique(test$names))
getPalette <- colorRampPalette(brewer.pal(colourCount, "Set3"))

ggplot() + 
      geom_bar(data=test.melt, aes(x = Treatment, y = Abundance, fill = Species), stat="identity", colour="black") +
      scale_fill_manual(values= getPalette(colourCount), breaks=n1) +
      xlab("Treatments") +
      ylab("Relative Abundance") +
      ggtitle("Some Title") +
      theme_bw() +
      theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"),
            legend.title = element_text(size=16, face="bold", vjust=20, hjust=10),
            plot.title = element_text(size=20, vjust = 2, face="bold")) 

ggplot() + 
      geom_bar(data=test.melt, aes(x = Treatment, y = Abundance, fill = Species), stat="identity", colour="black") +
      scale_fill_manual(values= getPalette(colourCount), breaks=n1) +
      xlab("Treatments") +
      ylab("Relative Abundance") +
      ggtitle("Some Title") +
      theme_bw() +
      theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"),
            legend.title = element_text(size=16, face="bold", vjust=2, hjust=1),
            plot.title = element_text(size=20, vjust = 2, face="bold")) 
库(ggplot2)
图书馆(重塑)
图书馆(RColorBrewer)

测试我不知道最近是否修复了此问题,但解决方案是在
theme()中使用
legend.title.align

用上面的例子

ggplot() + 
  geom_bar(data=test.melt, aes(x = Treatment, y = Abundance, fill = Species), 
   stat="identity", colour="black") +
  scale_fill_manual(values= getPalette(colourCount), breaks=n1) +
  xlab("Treatments") +
  ylab("Relative Abundance") +
  ggtitle("Some Title") +
  theme_bw() +
  theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"),
        legend.title = element_text(size=16, face="bold"), 
      legend.title.align=-30, #<<- here
        plot.title = element_text(size=20, vjust = 2, face="bold")) 
ggplot()+
geom_bar(数据=试验熔体,aes(x=处理,y=丰度,填充=种类),
stat=“identity”,color=“black”)+
缩放填充手动(值=获取调色板(颜色计数),断点=n1)+
xlab(“治疗”)+
ylab(“相对丰度”)+
ggtitle(“某些标题”)+
主题_bw()+
主题(axis.text=element\u text(size=12),axis.title=element\u text(size=14,face=“bold”),
legend.title=元素\文本(大小=16,面=“粗体”),

legend.title.align=-30、#From、
hjust
vjust
必须在[0,1]中。此外,请将调用添加到包
RColorBrewer
重塑
(或
重塑2
),当你使用它们时。谢谢。我试试看。根据语法,它是一个数字。@Pascal
vjust
hjust
值可以超出[0,1]范围。此外,你也可以使用负值。例如,当你使用
axis.text=element\u text(size=12,vjust=-1.5)时
您可以看到效果。@Jaap我刚刚阅读了手册。您想要达到什么效果?如果是为了在图例标题和图例之间留出一些空间,您可以使用的一种解决方法是在图例标题中添加一条白线:
缩放填充手册(“种类”\n,“值”=getPalette(colorCount),breaks=n1)
“图例.title.align=-30”在我的体形中水平调整而不垂直偏移。