ggplot and axis numbers and labels

后端 未结 2 1565
[愿得一人]
[愿得一人] 2020-12-18 14:39

i tried to build the following sample plot (see here for an example of a CIELAB Color Wheel). It is nearly finished, but there are still some small issues (see demo): - the

2条回答
  •  情深已故
    2020-12-18 14:51

    I don't know of any way to do this besides creating the text manually by using theme(axis.text=element_blank(),axis.ticks=element_blank()) to remove the old axes and geom_text() to add the new axis labels. I've modified your code below:

    library(ggplot2)
    circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
      r = diameter / 2
      tt <- seq(0,2*pi,length.out = npoints)
      xx <- center[1] + r * cos(tt)
      yy <- center[2] + r * sin(tt)
      return(data.frame(x = xx, y = yy))
    }
    
    # sample data for yellow
    colorvals <- data.frame(file = 'Yellow.csv', L = 88.94026, a = -9.8599137, b=88.77139)
    
    # build the circles for the plot
    r20 <- circleFun(center = c(0, 0), diameter = 40, npoints = 100)
    r40 <- circleFun(center = c(0, 0), diameter = 80, npoints = 100)
    r60 <- circleFun(center = c(0, 0), diameter = 120, npoints = 100)
    r80 <- circleFun(center = c(0, 0), diameter = 160, npoints = 100)
    r100 <- circleFun(center = c(0, 0), diameter = 200, npoints = 100)
    r120 <- circleFun(center = c(0, 0), diameter = 240, npoints = 100)
    dat <- rbind(r20, r40, r60, r80, r100, r120)
    
    # define labels and coordinates for axes
    y.labs <- data.frame(x=rep(0,11),y=seq(-100,100,by=20))
    y.text <- as.character(y.labs[,2])
    y.text[6] <- ""
    x.labs <- data.frame(x=seq(-100,100,by=20),y=rep(0,11))
    x.text <- as.character(x.labs[,1])
    x.text[6] <- ""
    
    # plot the data
    ggplot(data = dat, aes(x, y)) +
      geom_path() +
      geom_hline() +
      geom_vline() +
      theme(axis.text=element_blank(),axis.ticks=element_blank(),legend.position = c(1,0), legend.justification=c(1,0)) +
      xlab("a* (Grün/Rot)") +
      ylab("b* (Gelb/Blau)") +
      labs(colour="L*") +
      geom_point(data = colorvals, aes(x = a, y = b), size=3) +
      geom_text(data = colorvals, aes(x = a, y = b, label = gsub(".csv", "", file)), size = 3, vjust=0,hjust=1.2) +
      geom_text(data=x.labs, aes(x = x, y = y,label = x.text),vjust=0,hjust=.5) +
      geom_text(data=y.labs, aes(x = x, y = y,label = y.text),vjust=.5,hjust=1)
    

提交回复
热议问题