相关疑难解决方法(0)

绘制2 y轴,左侧为y轴,右侧为y轴

我需要绘制一个显示计数的条形图和一个在一个图表中显示速率的折线图,我可以分别做两个,但是当我把它们放在一起时,我的第一层的比例(即geom_bar)与第二层重叠层(即geom_line).

我可以将轴geom_line向右移动吗?

r ggplot2 r-faq

209
推荐指数
12
解决办法
25万
查看次数

如何使用具有双y轴ggplot的facet

我一直试图从这里扩展我的场景以利用facets(特别是facet_grid()).

我已经看过这个例子,但是我似乎无法让它为我geom_bar()geom_point()组合工作.我试图从仅仅改变例子中使用的代码facet_wrapfacet_grid这也似乎让第一层不显示.

对于网格和凹凸,我是一个非常新手,所以如果有人可以提供一些指导,如何使P1显示左侧y轴,P2显示在右侧y轴上,这将是很好的.

数据

library(ggplot2)
library(gtable)
library(grid)
library(data.table)
library(scales)

grid.newpage()

dt.diamonds <- as.data.table(diamonds)

d1 <- dt.diamonds[,list(revenue = sum(price),
                        stones = length(price)),
                  by=c("clarity","cut")]

setkey(d1, clarity,cut)
Run Code Online (Sandbox Code Playgroud)

p1和p2

p1 <- ggplot(d1, aes(x=clarity,y=revenue, fill=cut)) +
  geom_bar(stat="identity") +
  labs(x="clarity", y="revenue") +
  facet_grid(. ~ cut) +
  scale_y_continuous(labels=dollar, expand=c(0,0)) + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1),
        axis.text.y = element_text(colour="#4B92DB"), 
        legend.position="bottom")

p2 <- ggplot(d1, aes(x=clarity, y=stones, colour="red")) +
  geom_point(size=6) + 
  labs(x="", y="number …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gtable

27
推荐指数
2
解决办法
6652
查看次数

使用grid.arrange保留图形的比例

我正在尝试使用安排多个地块grid.arrange.它完成了本书的工作,并在致电时:

p1 <- ggplot(subset(mtcars, cyl = 4), aes(wt, mpg, colour = cyl)) + geom_point() 
p2 <- ggplot(subset(mtcars, cyl = 8), aes(wt, mpg, colour = cyl)) + geom_point()

grid.arrange(p1, p2, ncol = 2)
Run Code Online (Sandbox Code Playgroud)

我得到两个很好的图,大小对称:

在此输入图像描述

我的图表引用了不同的参数,但它们对组共享相同的颜色编码.所以我想删除除了一个以外的所有传奇,找到一个不错的地方.

但是,当我尝试:

p3 <- ggplot(subset(mtcars, cyl = 8), aes(wt, mpg, colour = cyl)) + geom_point() + guides(colour=FALSE)

grid.arrange(p3, p2, ncol = 2)
Run Code Online (Sandbox Code Playgroud)

没有图例的情节得到(正确)更大:

在此输入图像描述

我想保持大小(作为x轴的长度)在图表中保持相同.

我知道我可以在这里使用faceting,但是我还需要结合使用facet(我认为)很难实现的各种图形.

有可能做到grid.arrange吗?还有其他解决方案吗?

r ggplot2

19
推荐指数
2
解决办法
1万
查看次数

在ggplot中对齐图的危险

如何组合单独的图(ggplot2),不同的y轴和不同的绘图高度,但保持对齐?

详情

将图表与grid.arrange(方法1)组合在一起时,使用不同的y轴单位时,它们不会对齐.解决这个问题的一种方法是使用gtable(method2),但我无法调整图的相对高度.

require(ggplot2)

#Make two plots, with different y axis
  x = c(1, 5)
  y= c(.1, .4)
  data1<-data.frame(x,y)
  top<-
    ggplot(data1, aes(x=x, y=y))+
    geom_line()

  x = c(1, 5)
  y= c(100000, 400000)
  data2<-data.frame(x,y)
  bottom<-
    ggplot(data2, aes(x=x, y=y))+
    geom_line()


# Method 1 - Grid Extra 
  require(gridExtra)
  grid.arrange(top, bottom, heights=c(.6,.3))
Run Code Online (Sandbox Code Playgroud)

方法1导致此图,由于y轴标签的长度不同,该图未对齐:

在此输入图像描述

#Method 2 - gtable
  require(gtable)
  #Extract Grobs
  g1<-ggplotGrob(top)
  g2<-ggplotGrob(bottom)
  #Bind the tables
  g<-gtable:::rbind_gtable(g1, g2, "first")
  #Remove a row between the plots
  g <- gtable_add_rows(g, unit(-1,"cm"), pos=nrow(g1)) …
Run Code Online (Sandbox Code Playgroud)

r alignment ggplot2 gridextra gtable

16
推荐指数
1
解决办法
3552
查看次数

ggplot2:在绘图顶部添加二次变换的x轴

[ 编辑2016年4月:此线程中的解决方案不再正确显示添加的轴 - 此问题的新线程已在ggplot2打开2.1.0打破了我的代码?二次变换轴现在显示不正确 ]

我正在使用缩放的x数据,并且需要在绘图顶部添加一个未缩放的x轴以便于解释.我遇到了一个添加辅助y轴的方法我如何在ggplot2的右侧放置一个转换后的比例?.但是,我无法让它在x轴上正常工作.我确定我不理解代码的某些部分,但我似乎无法弄清楚它是什么.我已经尝试过查看ggplot2帮助文件,以及Wickham书籍ggplot2:数据分析的优雅图形,但如果有人能指出我的一些相关文档,我会非常感激!

我正在使用温度数据,但是我将使用上面链接中的湖泊数据作为代码.以下是该链接的原始代码:

library(ggplot2)
library(gtable)
library(grid)
LakeLevels<-data.frame(Day=c(1:365),Elevation=sin(seq(0,2*pi,2*pi/364))*10+100)
p1 <- ggplot(data=LakeLevels) + geom_line(aes(x=Day,y=Elevation)) + 
      scale_y_continuous(name="Elevation (m)",limits=c(75,125))

p2<-ggplot(data=LakeLevels)+geom_line(aes(x=Day, y=Elevation))+
    scale_y_continuous(name="Elevation (ft)", limits=c(75,125),           
    breaks=c(80,90,100,110,120),
             labels=c("262", "295", "328", "361", "394"))

#extract gtable
g1<-ggplot_gtable(ggplot_build(p1))
g2<-ggplot_gtable(ggplot_build(p2))

#overlap the panel of the 2nd plot on that of the 1st plot

pp<-c(subset(g1$layout, name=="panel", se=t:r))
g<-gtable_add_grob(g1, g2$grobs[[which(g2$layout$name=="panel")]], pp$t, pp$l, pp$b, 
                   pp$l)

ia <- which(g2$layout$name == "axis-l")
ga <- g2$grobs[[ia]]
ax <- ga$children[[2]]
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs) …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

12
推荐指数
1
解决办法
1万
查看次数

ggplot2中的双y轴用于多个面板图

我们不能用ggplot2轻松创建双y轴.

如果数字有多个面板(由geom_wrap或geom_grid创建),例如下面的4面板图,则这些技巧无法工作.我想将p1的轴添加到合并图的面板的右侧.

编辑:我在这里删除了我的代码并发布我自己的解决方案作为答案.以防有人需要它.

r ggplot2

9
推荐指数
1
解决办法
7940
查看次数

用双轴重现"经济学人"图表

我试图从经济学家(左边的那个)复制这个 图表.该图表绘制了左边y轴上俄罗斯亿万富翁的数量以及右边世界其他地区的亿万富翁数量.

  1. 为俄罗斯亿万富翁创建图表(p1).
  2. 为其他人创建图表(p2).
  3. 使用Kohske的代码组合p1p2进入双y轴图表.

数据:(内容billionaire.csv)

,Russia,World
1996,0,423
1997,4,220
1998,1,221
1999,0,298
2000,0,322
2001,8,530
2002,6,466
2003,17,459
2004,25,562
2005,27,664
2006,33,760
2007,53,893
2008,87,1038
2009,32,761
2010,62,949
2011,101,1109
2012,96,1130
2013,110,1317
2014,111,1535
2015,88,1738
Run Code Online (Sandbox Code Playgroud)

码:

library(ggplot2)
library(gtable)
library(grid)
library(extrafont) # for Officiana font
dat <- read.csv("billionaire.csv")
rus <- dat[,1:2]
world <- dat[,-2]

grid.newpage()
p1 <- ggplot(rus, aes(X, Russia)) + geom_line(colour = "#68382C", size = 1.5) + ggtitle("Number in Russia") +
  ylim(0, 200) …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gtable

6
推荐指数
1
解决办法
1175
查看次数

geom_bar + geom_line:具有不同的y轴刻度?

有没有办法用geom_line绘制geom_bar,如下图所示.

在此输入图像描述

我想出了两个独立的图表.如何将它们分别与左右两侧的两个不同轴组合.

library(ggplot2)
temp = data.frame(Product=as.factor(c("A","B","C")),
                  N = c(17100,17533,6756),
                  n = c(5,13,11),
                  rate = c(0.0003,0.0007,0.0016),
                  labels = c(".03%",".07%",".16%"))

p1 = ggplot(data = temp, aes(x=Product,y=N))+
  geom_bar(stat="identity",fill="#F8766D")+geom_text(aes(label=n,col="red",vjust=-0.5))+
  theme(legend.position="none",axis.title.y=element_blank(),axis.text.x = element_text(angle = 90, hjust = 1))
  p1
p2 = ggplot(data = temp,aes(x=Product,y=rate))+
  geom_line(aes(group=1))+geom_text(aes(label=labels,col="red",vjust=0))+
  theme(legend.position="none",axis.title.y=element_blank(),
        axis.text.x = element_text(angle = 90, hjust = 0))+
  xlab("Product")
p2
Run Code Online (Sandbox Code Playgroud)

非常感谢.

r ggplot2 geom-bar

5
推荐指数
2
解决办法
4276
查看次数

ggplot2 2.1.0破坏了我的代码?现在,辅助变换轴显示不正确

前一段时间,我询问是否要在ggplot中添加辅助变换的x轴,Nate Pope提供了ggplot2中描述的出色解决方案plot顶部添加辅助变换的x轴

该解决方案对我来说非常有效,我又返回了它,希望它可以用于新项目。不幸的是,该解决方案在ggplot2的最新版本中无法正常工作。现在,运行完全相同的代码将导致轴标题“剪切”,以及刻度线和标签重叠。这是一个示例,问题以蓝色突出显示:

在此处输入图片说明

可以使用以下代码来复制此示例(这是Nate Pope以前工作出色的代码的精确副本):

library(ggplot2)
library(gtable)
library(grid)

LakeLevels<-data.frame(Day=c(1:365),Elevation=sin(seq(0,2*pi,2*pi/364))*10+100)

## 'base' plot
p1 <- ggplot(data=LakeLevels) + geom_line(aes(x=Elevation,y=Day)) + 
  scale_x_continuous(name="Elevation (m)",limits=c(75,125)) +
  ggtitle("stuff") +
  theme(legend.position="none", plot.title=element_text(hjust=0.94, margin = margin(t = 20, b = -20)))

## plot with "transformed" axis
p2<-ggplot(data=LakeLevels)+geom_line(aes(x=Elevation, y=Day))+
  scale_x_continuous(name="Elevation (ft)", limits=c(75,125),
                     breaks=c(90,101,120),
                     labels=round(c(90,101,120)*3.24084) ## labels convert to feet
  )

## extract gtable
g1 <- ggplot_gtable(ggplot_build(p1))
g2 <- ggplot_gtable(ggplot_build(p2))

## overlap the panel of the 2nd plot …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gtable

5
推荐指数
2
解决办法
1030
查看次数

在ggplot2中绘制欧元符号€?

欧元符号显示为...PDF输出

ggplot() + theme_bw() + geom_line() + scale_y_continuous(formatter = "euro")
Run Code Online (Sandbox Code Playgroud)

r ggplot2 euro

3
推荐指数
1
解决办法
2419
查看次数

标签 统计

ggplot2 ×10

r ×10

gtable ×4

alignment ×1

euro ×1

geom-bar ×1

gridextra ×1

r-faq ×1