从 R 中的 GGparcoord 图中的轴中删除标签
Get rid of labels from an axis in GGparcoord graph in R
我想去除右轴上的标签,但保留 R 中内置的平行坐标图中左侧的标签。此外,右侧轴的轴与左侧轴略有不同- 侧轴。
是否可以 1. 去掉右侧标签和 2. 为右侧变量获取另一个轴?
这是一个示例数据集:df
df <- structure(list(States = structure(c(1L, 3L, 2L, 4L),
.Label = c("AP", "Gujarat", "Punjab", "Rajasthan"),
class = "factor"),
cases = c(20L, 45L, 15L, 10L),
vacancy = c(14L, 67L, 45L, 5L)),
class = "data.frame",
row.names = c(NA, -4L))
这是我的代码:
require(dplyr)
require(tidyverse)
library(GGally)
library(ggplot2)
df = df
p <- ggparcoord(df, columns=c(2,3),groupColumn = 1, scale="globalminmax", showPoints = TRUE, title = "cases vs vacancy",
alphaLines = 0.02, mapping=aes(color="black")) +
theme(panel.grid.major.x=element_line()) +
theme(
panel.border = element_blank(),
#panel.grid.major = element_blank(),
#panel.grid.minor = element_blank(),
panel.background = element_blank()
#axis.line = element_line()
) +
#theme_void() +
geom_line() + geom_text(aes(label = States), color = 'grey',size=3, hjust =1.2) + guides(color = FALSE, size = FALSE)
p
这是我的图表到目前为止的样子..
问题是 ggparcoord
做了一些数据处理,尤其是将宽数据帧转换为长格式。如果您使用 geom_text
,它会从原始图中继承 data
和 aes
。 data
是长格式,因此它会在该行的两边生成一个标签。您可以做的是从绘图中获取数据并跳过绘图右侧部分的行:
p <- ggparcoord(df,
columns = c(2, 3),
groupColumn = 1,
scale = "globalminmax",
showPoints = TRUE,
title = "cases vs vacancy",
alphaLines = 0.02,
mapping = aes(color = "black")) +
theme(panel.grid.major.x = element_line()) +
theme(
panel.border = element_blank(),
panel.background = element_blank()
) +
geom_line() +
guides(color = FALSE, size = FALSE)
(pnew <- p +
geom_text(aes(label = States),
## here's the trick use the data from the plot and prune it
data = p$data[p$data$variable == p$data$variable[1], ],
color = "grey",
size = 3,
hjust = 1.2))
关于第二轴,可以通过
获取
pnew + scale_y_continuous(sec.axis = dup_axis())
你可以试试这个
df %>% gather(key = "stage", value = "diff", -id)%>% fiter(stage == "after") %>% ggplot(aes(stage, diff)) + geom_point(aes(group = id)) + geom_line(aes(group = id)) + theme(axis.line.x = element_blank(), axis.ticks.x = element_blank()) + scale_x_discrete(name = NULL) + scale_y_continuous(name = NULL, position = "right") + geom_text(aes(x = stage, y = diff, label = id), data = left, nudge_x = -0.02)
我想去除右轴上的标签,但保留 R 中内置的平行坐标图中左侧的标签。此外,右侧轴的轴与左侧轴略有不同- 侧轴。
是否可以 1. 去掉右侧标签和 2. 为右侧变量获取另一个轴?
这是一个示例数据集:df
df <- structure(list(States = structure(c(1L, 3L, 2L, 4L),
.Label = c("AP", "Gujarat", "Punjab", "Rajasthan"),
class = "factor"),
cases = c(20L, 45L, 15L, 10L),
vacancy = c(14L, 67L, 45L, 5L)),
class = "data.frame",
row.names = c(NA, -4L))
这是我的代码:
require(dplyr)
require(tidyverse)
library(GGally)
library(ggplot2)
df = df
p <- ggparcoord(df, columns=c(2,3),groupColumn = 1, scale="globalminmax", showPoints = TRUE, title = "cases vs vacancy",
alphaLines = 0.02, mapping=aes(color="black")) +
theme(panel.grid.major.x=element_line()) +
theme(
panel.border = element_blank(),
#panel.grid.major = element_blank(),
#panel.grid.minor = element_blank(),
panel.background = element_blank()
#axis.line = element_line()
) +
#theme_void() +
geom_line() + geom_text(aes(label = States), color = 'grey',size=3, hjust =1.2) + guides(color = FALSE, size = FALSE)
p
这是我的图表到目前为止的样子..
问题是 ggparcoord
做了一些数据处理,尤其是将宽数据帧转换为长格式。如果您使用 geom_text
,它会从原始图中继承 data
和 aes
。 data
是长格式,因此它会在该行的两边生成一个标签。您可以做的是从绘图中获取数据并跳过绘图右侧部分的行:
p <- ggparcoord(df,
columns = c(2, 3),
groupColumn = 1,
scale = "globalminmax",
showPoints = TRUE,
title = "cases vs vacancy",
alphaLines = 0.02,
mapping = aes(color = "black")) +
theme(panel.grid.major.x = element_line()) +
theme(
panel.border = element_blank(),
panel.background = element_blank()
) +
geom_line() +
guides(color = FALSE, size = FALSE)
(pnew <- p +
geom_text(aes(label = States),
## here's the trick use the data from the plot and prune it
data = p$data[p$data$variable == p$data$variable[1], ],
color = "grey",
size = 3,
hjust = 1.2))
关于第二轴,可以通过
获取pnew + scale_y_continuous(sec.axis = dup_axis())
你可以试试这个
df %>% gather(key = "stage", value = "diff", -id)%>% fiter(stage == "after") %>% ggplot(aes(stage, diff)) + geom_point(aes(group = id)) + geom_line(aes(group = id)) + theme(axis.line.x = element_blank(), axis.ticks.x = element_blank()) + scale_x_discrete(name = NULL) + scale_y_continuous(name = NULL, position = "right") + geom_text(aes(x = stage, y = diff, label = id), data = left, nudge_x = -0.02)