如何使用ggplot2创建一维图表?
How to create a one-dimensional chart with ggplot2?
我想做的是复制此图表的一行:
不必完全相同,但遵循相同的想法。我也希望能够画一条线。类似于:
----Y--Y--Y--|--N--N--N--N----
到目前为止,我得到的最接近的是使用 ggplot2 的 theme()
来隐藏轴,但没有想出我喜欢的东西。
如果您提供您正在处理的数据的简短样本,问题将更容易回答。假设你有这样的东西:
votes <- data.frame(vote=paste(1:7),
how=c(rep("Y", 3), rep("N", 4)),
legislator=c(rep("L1", 7) ) )
您可以创建这样的图表:
ggplot(votes, aes(x=vote, y=legislator) ) +
geom_text(aes(label=how)) +
geom_hline(aes(yintercept=1.05)) +
geom_segment(x=3.5, xend=3.5, y=0.95, yend=1.05, size=1.5)
如果你想要一个空的主题,从这样的开始:(来自https://gist.github.com/dsparks/3711166)
new_theme_empty <- theme_bw()
new_theme_empty$line <- element_blank()
new_theme_empty$rect <- element_blank()
new_theme_empty$strip.text <- element_blank()
new_theme_empty$axis.text <- element_blank()
new_theme_empty$plot.title <- element_blank()
new_theme_empty$axis.title <- element_blank()
new_theme_empty$plot.margin <- structure(c(0, 0, -1, -1),
unit = "lines",
valid.unit = 3L,
class = "unit")
并像这样使用它:
ggplot(votes, aes(x=vote, y=legislator) ) +
geom_text(aes(label=how)) +
geom_hline(aes(yintercept=1.05)) +
geom_segment(x=3.5, xend=3.5, y=0.95, yend=1.05, size=1.5) +
new_theme_empty
我想做的是复制此图表的一行:
不必完全相同,但遵循相同的想法。我也希望能够画一条线。类似于:
----Y--Y--Y--|--N--N--N--N----
到目前为止,我得到的最接近的是使用 ggplot2 的 theme()
来隐藏轴,但没有想出我喜欢的东西。
如果您提供您正在处理的数据的简短样本,问题将更容易回答。假设你有这样的东西:
votes <- data.frame(vote=paste(1:7),
how=c(rep("Y", 3), rep("N", 4)),
legislator=c(rep("L1", 7) ) )
您可以创建这样的图表:
ggplot(votes, aes(x=vote, y=legislator) ) +
geom_text(aes(label=how)) +
geom_hline(aes(yintercept=1.05)) +
geom_segment(x=3.5, xend=3.5, y=0.95, yend=1.05, size=1.5)
如果你想要一个空的主题,从这样的开始:(来自https://gist.github.com/dsparks/3711166)
new_theme_empty <- theme_bw()
new_theme_empty$line <- element_blank()
new_theme_empty$rect <- element_blank()
new_theme_empty$strip.text <- element_blank()
new_theme_empty$axis.text <- element_blank()
new_theme_empty$plot.title <- element_blank()
new_theme_empty$axis.title <- element_blank()
new_theme_empty$plot.margin <- structure(c(0, 0, -1, -1),
unit = "lines",
valid.unit = 3L,
class = "unit")
并像这样使用它:
ggplot(votes, aes(x=vote, y=legislator) ) +
geom_text(aes(label=how)) +
geom_hline(aes(yintercept=1.05)) +
geom_segment(x=3.5, xend=3.5, y=0.95, yend=1.05, size=1.5) +
new_theme_empty