Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.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 使用ggplot在图表中左对齐副标题_R_Ggplot2_Formatting_Leftalign - Fatal编程技术网

R 使用ggplot在图表中左对齐副标题

R 使用ggplot在图表中左对齐副标题,r,ggplot2,formatting,leftalign,R,Ggplot2,Formatting,Leftalign,有人知道我做的这张图表的副标题怎么左对齐吗?除了使用ggplot,我没有使用任何方法 library(ggplot2) library(ggthemes) library(grid) # Random exponential sample set.seed(10) n=20 y<-rexp(n=n) y<-y[order(y)] x<-seq(n)+1990 mydata<-data.frame(cbind(x,y)) # Plot p <- (ggplot(m

有人知道我做的这张图表的副标题怎么左对齐吗?除了使用ggplot,我没有使用任何方法

library(ggplot2)
library(ggthemes)
library(grid)

# Random exponential sample
set.seed(10)
n=20
y<-rexp(n=n)
y<-y[order(y)]
x<-seq(n)+1990
mydata<-data.frame(cbind(x,y))

# Plot
p <- (ggplot(mydata, aes(x=x, y=y))
  + geom_point(size=3,alpha=.50)
  + geom_smooth(method="lm",formula=y~poly(x,2,raw=T),se=F,size=1)
  + theme_economist(base_size=12, base_family="Avenir")
  + labs(title=expression(atop(bold("Inequality Is Increasing"), atop("Gini Coefficient", ""))))
  + labs(x="")
  + labs(y="")
  + annotate("text", label = "Source:World Bank Data", x = 2009, y = Inf, vjust = 61, size=4)
)

# Overide clipping
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)
库(ggplot2)
图书馆(主题)
图书馆(网格)
#随机指数样本
种子(10)
n=20

y我对你的代码做了很多实验。当您想要有字幕时,您的原始代码运行良好。但你牺牲的是左对齐。如果希望左对齐,可以执行类似于
ggtitle(“不平等性在增加\nGini系数”)
的操作。但是,例如,你牺牲了字体风格。这是一种权衡。我能想到的唯一解决办法是把副标题和主标题分开。我把这些微妙的东西当作一篇文章。一、 因此,使用注释。我使用x和y值来确定字幕的最佳位置。这纯粹是实验性的、乏味的。但是,我想这是我能为你提供的最好的了。我希望这能满足你的要求

# Plot
p <- (ggplot(mydata, aes(x=x, y=y))
  + geom_point(size=3,alpha=.50)
  + geom_smooth(method="lm",formula=y~poly(x,2,raw=T),se=F,size=1)
  + theme_economist(base_size=12, base_family="Avenir")
  + ggtitle("Inequality Is Increasing")
  + annotate("text", x = 1992.25, y = 3.2, label = "Gini Coefficient", fontface = 3)
  + labs(x="")
  + labs(y="")
  + annotate("text", label = "Source:World Bank Data", x = 2009, y = Inf, vjust = 61, size=4)
)
#绘图

p问题似乎是由于您使用了“顶部”而产生的。你需要这个功能吗?不需要。这是我知道如何添加副标题HI的唯一方法,
ggtitle(“不平等正在增加\nGini系数”)
允许您进行左对齐。但是,这个解决方案并不完美,因为您的副标题字体大小还没有变小。但我想至少是朝着你的目标迈出了一步。是的,谢谢爵士音乐,但我确实需要字体大小不同,所以看起来像副标题。这是一个伪副标题。但是,你有你想要的视觉效果!这对我来说也是一次很好的学习经历。哇!太好了。谢谢爵士音乐。非常感谢!