我希望两个不同的嵌套分组变量的级别出现在图表下方的单独行中,而不是在图例中.我现在拥有的是这段代码:
data <- read.table(text = "Group Category Value
S1 A 73
S2 A 57
S1 B 7
S2 B 23
S1 C 51
S2 C 87", header = TRUE)
ggplot(data = data, aes(x = Category, y = Value, fill = Group)) +
geom_bar(position = 'dodge') +
geom_text(aes(label = paste(Value, "%")),
position = position_dodge(width = 0.9), vjust = -0.25)
Run Code Online (Sandbox Code Playgroud)

我想拥有的是这样的:

有任何想法吗?
我想沿水平轴显示月份(缩写形式),相应的年份打印一次.我知道如何显示月份:
今年不需要的重复使标签变得混乱.相反,我想要这样的事情:
除了年份将 在月份以下打印.
我在轴标签上方打印了一年,因为这是我能做的最好的.这遵循annotate()函数的限制,如果它位于绘图区域之外,则会被剪裁.我知道可能的解决方法基于annotate_custom(),但我无法使它们与日期对象一起工作(我没有尝试将日期转换为数字并再次返回日期,因为它似乎比希望必要更复杂)
我想知道新的dup_axis()是否可以为此目的而被劫持.如果不是将复制的轴发送到面板的另一侧,它可以在复制的轴下面发送几行,那么可能只是设置一个轴被panel.grid.major清空并且标签设置为%b,而另一个轴将panel.grid.minor消隐,标签设置为%Y.(另外一个挑战是年标签将转移到10月而不是1月)
这些问题是相关的.但是,就我所知,annotate_custom()功能和textGrob()功能与日期不相符.
数据和基本代码如下:
library("ggplot2")
library("scales")
ggplot(data = df, aes(x = Date, y = value)) + geom_line() +
scale_x_date(date_breaks = "2 month", date_minor_breaks = "1 month", labels = date_format("%b %Y")) +
xlab(NULL)
ggplot(data = df, aes(x = Date, y = value)) + geom_line() +
scale_x_date(date_minor_breaks = "2 month", labels = date_format("%b")) +
annotate(geom = …Run Code Online (Sandbox Code Playgroud) 我有一张桌子,我想用ggplot2制作一个情节,到目前为止我还没有成功.我准备了一个看起来像这样的简化表
df1<-data.frame(Loc=c(rep("L1",5),rep("L2",3),rep("L3",4)),
Type=c(rep("T1",3),rep("T2",2),"T1","T2","T2","T1","T1","T2","T2"),
y2009=rep("A",12),y2010=c("A","B","A","A","A","A","B","B","A","A","B","B"),
y2011=c("B","B","B","A","B",rep("B",4),"A","B","B"))
df1
Run Code Online (Sandbox Code Playgroud)
Loc有3个位置.每个位置有2种类型的样本T1或T2.他们从2009年开始作为A,随着时间的推移,有些人成为B.因此,到2011年,有很多B.
这是我到目前为止的数字
ggplot(df1,aes(x=Type)) + geom_bar()+facet_grid(~Loc)
ggplot(df1,aes(x=y2009,fill=Type)) + geom_bar(position="dodge")+facet_grid(~Loc)
Run Code Online (Sandbox Code Playgroud)

我不太确定如何从三个因素中得到计数.
我想要一个类似于下面的数字,我粗略地在油漆中起草.facet是位置,我只为Loc1制作了条形图.

在"图形"包中,可以向直方图添加第二个x轴(表示分布的百分位数),如下所示:
x <- rnorm(1000)
hist(x, main="", xlab="Bias")
perc <- quantile(x, seq(from=.00, to=1, by=.1))
axis(1,at=perc,labels=c("0","10%","20%","30%","40%","50%","60%","70%","80%","90%","100%"),cex=0.5, pos= -90)
Run Code Online (Sandbox Code Playgroud)
当然,这看起来很尴尬.那么我怎么能修改下面的ggplot2代码来添加第二个x轴,甩掉百分位数,而第一个x轴应该指示原始值?:
library(ggplot2)
theme_classic(base_size = 12, base_family = "")
x <- rnorm(1000)
qplot(x, main="", xlab="Bias")
perc <- quantile(x, seq(from=.00, to=1, by=.1))
Run Code Online (Sandbox Code Playgroud)
有帮助吗?提前谢谢了!
我想在ggplot中制作两行X轴标签.

在这个图中,我想在每个指定年份下面再添加一行标签.就像是
1990 1995 2000 2005 2010
cold warm warm cold warm
这是我制作这个情节的代码
ggplot(subset(dat, countryid %in% c("1")), aes(date,
nonpartisan))+geom_line(aes(color=countryid), color="dodgerblue1",
size=1.4)+geom_line(aes(date, reshuffle), color="gray")+ theme_bw()
Run Code Online (Sandbox Code Playgroud)
有没有办法通过专门为标签创建一个列来创建一个标签行?
谢谢!
尽管以欧元和 PPS 为最低工资。该分组适用于以欧元为单位的值。一切正常,但我不明白如何在 x 轴上额外添加“组”。这是我的代码
library(eurostat)
library(tidyverse)
library(ggplot2)
dat_MW <- get_eurostat(id="earn_mw_cur", time_format="num")
dat_MW <- label_eurostat(dat_MW)
dat_MW_w <- dat_MW %>% filter(time==2017, currency %in% c("Euro","Purchasing Power Standard")) %>% arrange(currency, values)
dat_MW_w$geo[dat_MW_w$geo=="Germany (until 1990 former territory of the FRG)"] <- "Germany"
dat_MW_w$geo[dat_MW_w$geo=="Former Yugoslav Republic of Macedonia, the"] <- "Macedonia"
dat_MW_w$currency[dat_MW_w$currency=="Purchasing Power Standard"] <- "PPS"
dat_MW_w$currency[dat_MW_w$currency=="Euro"] <- "EUR"
dat_MW_w <- dat_MW_w %>%
mutate(group=ifelse(values<=500 & currency=="EUR","GROUP1",
ifelse(values<=1000 & currency=="EUR", "GROUP2",
ifelse(currency=="EUR","GROUP3", NA))))
figure1 <- ggplot(data=dat_MW_w, aes(x=reorder(geo, values), y=values, group=currency)) +
xlab("Countries") + ylab("EUR/PPS") + …Run Code Online (Sandbox Code Playgroud) 我的数据采用以下结构:
month <- c("May", "June", "July", "May", "June", "July")
year <- c("2015", "2015", "2015", "2016", "2016", "2016")
value <- c(1:3, 3:1)
df <- data.frame(month, year, value)
Run Code Online (Sandbox Code Playgroud)
(实际上,这两年的数据都从 1 月到 12 月,这只是一个可重复的简短示例。)
我正在做一个value使用 ggplot的时间序列图(假设我plot.ts()因为太复杂而无法在这里解释的原因而无法使用)。我如何将 x 轴的标签分层,以便每个刻度线都标有月份,但在其下方还有另一个带有年份的标签,所以我得到类似的信息:
-------+-------+-------+----//---+-------+-------+-----
May June July May June July
2015 2016
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) 我尝试使用两次调用 annotate() 的技巧来获取 2 级嵌套刻度标签(此处),但我想与 facet_wrap() 结合使用
df <- tribble(
~action, ~gend, ~status, ~cellMean,
"P", "M", "A", 1,
"P", "M", "B", 2,
"P", "F", "A", 1.4,
"P", "F", "B", 2.6,
"V", "M", "A", 2,
"V", "M", "B", 3,
"V", "F", "A", 2.2,
"V", "F", "B", 3.8
) %>%
mutate(action=factor(action,levels=c("P","V")),
gend =factor(gend, levels=c("F","M")),
status=factor(status,levels=c("A","B")))
df
# action obscured/overlaid
df %>%
ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
color=status, shape=gend)) +
geom_point(size=3.5) +
theme_light()
Run Code Online (Sandbox Code Playgroud)
# action used as facet
df %>%
ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean, …Run Code Online (Sandbox Code Playgroud) ggplot2 ×10
r ×10
axis-labels ×3
bar-chart ×1
date ×1
facet-grid ×1
facet-wrap ×1
gtable ×1
histogram ×1
label ×1
multi-level ×1
x-axis ×1