编辑:此问题已被标记为重复,但此处的回复已经过尝试但无效,因为相关案例是折线图,而不是条形图.应用这些方法会生成一个包含5行的图表,每年1行 - 没用.投票标记为重复的任何人是否真的在这个问题提供的样本数据集上尝试这些方法?如果是这样,请发布作为答案.
原始问题:
Excel枢轴图中有一个功能,允许多级分类轴.我正在尝试找到一种方法ggplot
(或R中的任何其他绘图包).
请考虑以下数据集:
set.seed(1)
df=data.frame(year=rep(2009:2013,each=4),
quarter=rep(c("Q1","Q2","Q3","Q4"),5),
sales=40:59+rnorm(20,sd=5))
Run Code Online (Sandbox Code Playgroud)
如果将其导入Excel数据透视表,则可以直接创建以下图表:
注意x轴有两个级别,一个用于四分之一,一个用于分组变量year.是否可以使用多级轴ggplot
?
注意:有一个带有小平面的黑客会产生类似的东西,但这不是我想要的.
library(ggplot2)
ggplot(df) +
geom_line(aes(x=quarter,y=sales,group=year))+
facet_grid(.~year,scales="free")
Run Code Online (Sandbox Code Playgroud)
我试图在ggplot2中创建的图表下面显示一些有关数据的信息.我想使用图的X轴坐标绘制N变量,但Y坐标需要距离屏幕底部10%.实际上,期望的Y坐标已经作为y_pos变量存在于数据框中.
我可以想到使用ggplot2的3种方法:
1)在实际绘图下方创建一个空图,使用相同的比例,然后使用geom_text在空白图上绘制数据.这种方法有点有效,但非常复杂.
2)geom_text
用于绘制数据,但以某种方式使用y坐标作为屏幕的百分比(10%).这将强制数字显示在图表下方.我无法弄清楚正确的语法.
3)使用grid.text显示文本.我可以轻松地将它设置在屏幕底部的10%,但我无法确定如何设置X coordindate以匹配绘图.我试图使用grconvert捕获最初的X位置但是也无法使其工作.
以下是虚拟数据的基本情节:
graphics.off() # close graphics windows
library(car)
library(ggplot2) #load ggplot
library(gridExtra) #load Grid
library(RGraphics) # support of the "R graphics" book, on CRAN
#create dummy data
test= data.frame(
Group = c("A", "B", "A","B", "A", "B"),
x = c(1 ,1,2,2,3,3 ),
y = c(33,25,27,36,43,25),
n=c(71,55,65,58,65,58),
y_pos=c(9,6,9,6,9,6)
)
#create ggplot
p1 <- qplot(x, y, data=test, colour=Group) +
ylab("Mean change from baseline") +
geom_line()+
scale_x_continuous("Weeks", breaks=seq(-1,3, by = 1) …
Run Code Online (Sandbox Code Playgroud) 我有以下图表:
library(ggplot2)
library(scales)
library(magrittr)
df1 <-
structure(
list(
x = structure(
1:5, .Label = c("5", "4", "3", "2",
"1"), class = "factor"
), y = c(
0.166666666666667, 0.361111111111111,
0.0833333333333333, 0.222222222222222, 0.291666666666667
)
), .Names = c("x",
"y"), row.names = c(NA,-5L), class = c("tbl_df", "tbl", "data.frame"), drop = TRUE
)
df1 %>% ggplot(aes(x , y )) + geom_bar(stat = "identity") +
scale_y_continuous(labels = percent)
Run Code Online (Sandbox Code Playgroud)
我想在5和1之下添加带有粗体文本的两行注释.例如,'最高\nvalue'低于5,'最低\n值'低于1.
我试过geom_text
但我不能把文字放在我想要的地方.
我有以下数据集:
df <- data.frame(dens = rnorm(5000),
split = as.factor(sample(1:2, 5000, replace = T)),
method = as.factor(sample(c("A","B"), 5000, replace = T)),
counts = sample(c(1, 10, 100, 1000, 10000), 5000, replace = T))
Run Code Online (Sandbox Code Playgroud)
对于每个计数,我在 A 组和 B 组中为分割 1 和 2 提供了以下分割小提琴图。每个设置我们有四个组,但它有一个嵌套的方面:
library(ggplot2)
GeomSplitViolin <- ggproto("GeomSplitViolin", GeomViolin,
draw_group = function(self, data, ..., draw_quantiles = NULL){
## By @YAK: /sf/ask/2500214741/
data <- transform(data, xminv = x - violinwidth * (x - xmin), xmaxv = x + violinwidth * (xmax - x))
grp <- …
Run Code Online (Sandbox Code Playgroud) 这篇文章描述了一种在时间序列图上创建两线 x 轴(年份低于月份)的方法。不幸的是,我在这篇文章中使用的方法(选项 2)与ggsave()
.
library(tidyverse)
library(lubridate)
df <- tibble(
date = as.Date(41000:42000, origin = "1899-12-30"),
value = c(rnorm(500, 5), rnorm(501, 10))
)
p <- ggplot(df, aes(date, value)) +
geom_line() +
geom_vline(
xintercept = as.numeric(df$date[yday(df$date) == 1]), color = "grey60"
) +
scale_x_date(date_labels = "%b", date_breaks = "month", expand = c(0, 0)) +
theme_bw() +
theme(panel.grid.minor.x = element_blank()) +
labs(x = "")
# Get the grob
g <- ggplotGrob(p)
# Get the y axis
index …
Run Code Online (Sandbox Code Playgroud) big_theme <- theme(
panel.background = element_rect(fill = "black"),
plot.background = element_rect(fill = "black", colour = NA),
plot.title = element_text(hjust = 0.5, size = 15, color = "white"),
axis.text.y = element_text(colour = "white", size = 14),
axis.title.x = element_text(colour = "white", size = 14),
axis.title.y = element_text(colour = "white", size = 14),
axis.text.x = element_text(vjust = 1, angle = 45, color = "white", size = 14, hjust=1),
strip.background = element_rect(fill = "black"),
strip.text = element_text(colour = 'white'),
strip.text.y = element_text(angle = …
Run Code Online (Sandbox Code Playgroud)