如何仅显示极地ggplot的绘图区域的一部分? [英] How to show only part of the plot area of polar ggplot with facet?

查看:100
本文介绍了如何仅显示极地ggplot的绘图区域的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设在极坐标中有一个数据集被绘制为扇区

  library(ggplot2)
library( (数据$(rnorm(1000),nrow = 20))
data $ type <-1:2
data $ Var1 < - data $ Var1 * 6 - 60

ggplot(data,aes(Var1,Var2))+
geom_tile(aes(fill = value))+
coord_polar(theta =x, start = pi)+
scale_x_continuous(limits = c(-180,180))+
facet_wrap(〜type)

给出以下图表:




解决方案

这是一个不雅的黑客攻击,但您可以使用 grid 函数来掩盖您不想要的区域。例如:

 库(ggplot2)
库(reshape2)
库(网格)

data < - melt(matrix(rnorm(1000),nrow = 20))
data $ type <-1:2
data $ Var1 < - data $ Var1 * 6 - 60

p1 = ggplot(data,aes(Var1,Var2))+
geom_tile(aes(fill = value))+
coord_polar(theta =x (限制= c(-180,180))+
facet_wrap(〜type)
g1 = ggplotGrob(p1)

grid.new()
pushViewport(viewport(height = 1,width = 1,clip =on))
grid.draw(g1)
grid.rect(x = 0, y = 0,height = 1,width = 2,gp = gpar(col =white))



<这切断了图表的下半部分(见下文)。找到一个更优雅的方法会很好,但如果失败了,也许你可以使用视口布局和绘图功能(更不用说改变轴标签和图例的位置)来获得接近你想要的东西。


Assume there is a dataset in polar coordinate to be plotted as sector

library(ggplot2)
library(reshape2)
data <- melt(matrix(rnorm(1000), nrow = 20))
data$type <- 1:2
data$Var1 <- data$Var1*6 - 60

ggplot(data, aes(Var1, Var2)) + 
  geom_tile(aes(fill = value)) + 
  coord_polar(theta = "x", start = pi) + 
  scale_x_continuous(limits = c(-180, 180)) +
  facet_wrap(~type)

which gives the following graph:

How can we remove the bottom (blank) part of the plot while not making a full circle?

解决方案

This is an inelegant hack, but you can use grid functions to cover up the area you don't want. For example:

library(ggplot2)
library(reshape2)
library(grid)

data <- melt(matrix(rnorm(1000), nrow = 20))
data$type <- 1:2
data$Var1 <- data$Var1*6 - 60

p1 = ggplot(data, aes(Var1, Var2)) + 
  geom_tile(aes(fill = value)) + 
  coord_polar(theta = "x", start = pi) + 
  scale_x_continuous(limits = c(-180, 180)) +
  facet_wrap(~type)
g1 = ggplotGrob(p1)

grid.newpage()
pushViewport(viewport(height=1, width=1, clip="on"))
grid.draw(g1)
grid.rect(x=0,y=0,height=1, width=2, gp=gpar(col="white"))

This cuts off the bottom half of the graph (see below). It would be nice to find a more elegant approach, but failing that, maybe you can play around with viewport placement and drawing functions (not to mention changing the location of the axis labels and legend) to get something close to what you want.

这篇关于如何仅显示极地ggplot的绘图区域的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆