ggplot2 x - y 轴相交,同时保持轴标签 [英] ggplot2 x - y axis intersect while keeping axis labels

查看:19
本文介绍了ggplot2 x - y 轴相交,同时保持轴标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天发布了我原来的问题,在这里得到了完美的解决

I posted my original question yesterday which got solved perfectly here

原帖

我对我的代码做了一些补充

I made a few addition to my code

library(lubridate)
library(ggplot2)
library(grid)

### Set up dummy data.
dayVec     <- seq(ymd('2016-01-01'), ymd('2016-01-10'), by = '1 day')
dayCount   <- length(dayVec)
dayValVec1 <- c(0,-0.22,0.15,0.3,0.4,0.10,0.17,0.22,0.50,0.89)
dayValVec2 <- c(0,0.2,-0.17,0.6,0.16,0.41,0.55,0.80,0.90,1.00)
dayValVec3 <- dayValVec2
dayDF      <- data.frame(Date = rep(dayVec, 3),
                     DataType = factor(c(rep('A', dayCount), rep('B',   dayCount), rep('C', dayCount))),
                     Value = c(dayValVec1, dayValVec2, dayValVec3))


ggplot(dayDF, aes(Date, Value, colour = DataType)) +
theme_bw() +
ggtitle("Cumulative Returns 
") +
scale_color_manual("",values = c("#033563", "#E1E2D2", "#4C633C"),
                    labels = c("Portfolio ", "Index ", "In-Sample ")) +
geom_rect(aes(xmin = ymd('2016-01-01'),
            xmax = ymd('2016-01-06'),
            ymin = -Inf,
            ymax = Inf
            ), fill = "#E1E2D2", alpha = 0.03, colour = "#E1E2D2") +
geom_line(size = 2) +
scale_x_datetime(labels = date_format('%b-%d'), 
               breaks = date_breaks('1 day'),
               expand = c(0,0)) +
scale_y_continuous( expand = c(0,0), labels = percent) +
theme(axis.text.x  = element_text(angle = 90), 
    axis.title.x = element_blank(), 
    axis.title.y = element_blank(),
    panel.grid.minor = element_blank(),
    panel.grid.major.x = element_blank(),
    axis.line  = element_line(size = 1),
    axis.ticks = element_line(size = 1),
    axis.text  = element_text(size = 20, colour = "#033563"),
    axis.title.x = element_text(hjust = 2),
    plot.title = element_text(size = 40, face = "bold", colour = "#033563"),
    legend.position = 'bottom',
    legend.text = element_text(colour = "#033563", size = 20),
    legend.key  = element_blank()
    )

产生这个输出

唯一我仍然无法工作的是 x 轴的位置.我希望 x 轴位于 y = 0 但仍将 x 轴标签保留在图表下方,与它的 excel 版本完全相同.我知道数据集不一样,但我手头没有原始数据,所以我制作了一些虚拟数据.希望这值得提出一个新问题,谢谢.

The only thing that I still cannot get working is the position of the x axis. I want the x axis to be at y = 0 but still keep the x axis labels under the chart, exactly as in the excel version of it. I know the data sets are not the same but I didn't have the original data at hand so I produced some dummy data. Hope this was worth a new question, thanks.

> grid.ls(grid.force())
GRID.gTableParent.12660
background.1-5-7-1
spacer.4-3-4-3
panel.3-4-3-4
grill.gTree.12619
  panel.background.rect.12613
  panel.grid.minor.y.zeroGrob.12614
  panel.grid.minor.x.zeroGrob.12615
  panel.grid.major.y.polyline.12617
  panel.grid.major.x.zeroGrob.12618
geom_rect.rect.12607
GRID.polyline.12608
panel.border.rect.12610
axis-l.3-3-3-3
axis.line.y.polyline.12631
axis
axis-b.4-4-4-4
axis.line.x.polyline.12624
axis
xlab.5-4-5-4
ylab.3-2-3-2
guide-box.6-4-6-4
title.2-4-2-4
> grid.gget("axis.1-1-1-1", grep=T)
NULL

推荐答案

ggplot2 并不容易.下面是一种以交互方式处理此问题的方法.基本上,您只需抓住绘图的相关部分(轴线和刻度线)并重新定位它们.

ggplot2 doesn't make this easy. Below is one-way to approach this interactively. Basically, you just grab the relevant part of the plot (the axis line and ticks) and reposition them.

如果 p 是你的情节

p
grid.force()
# grab the relevant parts - have a look at grid.ls()
tck <- grid.gget("axis.1-1-1-1", grep=T)[[2]] # tick marks
ax <- grid.gget("axis.line.x", grep=T)        # x-axis line

# add them to the plot, this time suppressing the x-axis at its default position
p + lapply(list(ax, tck), annotation_custom, ymax=0) + 
          theme(axis.line.x=element_blank(), 
                axis.ticks.x=element_blank())

哪个产生

快速说明:ggplot2 的较新版本具有不显示轴的设计决定.此外,对 axis.line 的更改不会自动传递到 xy 轴.因此,我调整了您的主题以分别定义 axis.line.xaxis.line.y.

A quick note: the more recent versions of ggplot2 have the design decision to not show the axis. Also changes to axis.line are not automatically passed down to the x and y axis. Therefore, I tweaked your theme to define axis.line.x and axis.line.y separately.

那个 siad,也许更容易(并且更强大??)使用评论中建议的 geom_hlinegeom_segment 的刻度.

That siad, perhaps its easier (and more robust??) to use geom_hline as suggested in the comments, and geom_segment for the ticks.

这篇关于ggplot2 x - y 轴相交,同时保持轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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