简体   繁体   中英

Plotting x axes with increasing and decreasing x-values in R

I have been trying to plot x axes with increasing and the decreasing data values in R

ie x-axes have values from [60 to 90 to 60] , corresponding to different y-values

how can I do this?

This might not be an exact answer but you can use ggplot() and facet_wrap to achieve something similar:

library(ggplot2)
library(dplyr)
library(gridExtra)


# GGPLOT2
x = c(1:50, 50:1)
y = x + rnorm(1000)
group = rep(c("A", "B"), each = 50)

df <- data.frame(x, y, group)

p1 <- df %>% 
  filter(group == "A") %>% 
  ggplot(aes(x, y, color = "A")) + geom_point() + ggtitle("UP") + guides(color = F)

p2 <- df %>% 
  filter(group == "B") %>% 
  ggplot(aes(x, y, color = "B")) + geom_point() + scale_x_reverse() + ggtitle("DOWN") + guides(color = F)

grid.arrange(p1, p2, nrow = 1)

Recently the plotly package has been receiving a lot of attention. You could do the following using ploy_ly() :

# PLOTLY
library(plotly)

p <- df %>% 
  filter(group == "A") %>% 
  plot_ly(x = x, y = y, mode = "markers") %>% 
  layout(xaxis = list(domain = c(0, 0.5)))

p <- df %>% 
  filter(group == "A") %>% 
  add_trace(p, x = x, y = y, mode = "markers", xaxis = "x2", yaxis = "y2", data = .) %>% 
  layout(xaxis2 = list(autorange = "reversed",
                       domain = c(0.5, 1)),
         yaxis2 = list(overlaying = "y",
                       side = "right"))
p

Is this what you are looking for?

x = c(1,2,3,4,5,6,7,6,5,4,3,2,1)
y = c(4,5,6,7,8,11,12,23,45,25,11,16,2)
ggplot(data.frame(x=1:length(x), y))+
  geom_point(aes(x=x, y=y))+
  scale_x_discrete(labels = as.character(x))

在此处输入图片说明

This will not work so well if x is not in the proper order or has missing values. You should consider adding a MWE along with your attempts in your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM