Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
996 views
in Technique[技术] by (71.8m points)

r - ggplot2 x - y axis intersect while keeping axis labels

I posted my original question yesterday which got solved perfectly here

Original post

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()
    )

which produces this output

Graph

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

If p is your plot

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())

Which produces

enter image description here

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...