可视化平面上所有点的单个2d图

时间:2017-11-13 05:43:59

标签: python r matplotlib data-visualization

对于参数P和Q的给定值,我有一个M对N曲线(为了便于理解,我们将其视为一个sigmoid)。我需要将一系列P值的M对N曲线可视化和Q(假设0到1的10个值,即P和Q的0.1,0.2,...,0.9)

我发现这个问题的唯一解决方案是Trellis图(基本上是图表矩阵)。我想知道除了Trellis图之外是否有任何其他方法可视化这种4d(?)关系。感谢。

1 个答案:

答案 0 :(得分:2)

I'm not sure I understand what you're hoping for, so let me know if this is on the right track. Below are three examples using R.

The first is indeed a matrix of plots where each panel represents a different value of q and, within each panel, each curve represents a different value of p. The second is a 3D plot which looks at a surface based on three of the variables with the fourth fixed. The third is a Shiny app that creates the same interactive plot as in the second example but also provides a slider that allows you to change p and see how the plot changes. Unfortunately, I'm not sure how to embed the interactive plots in Stackoverflow so I've just provided the code.

I'm not sure if there's an elegant way to look at all four variables at the same time, but maybe someone will come along with additional options.

Matrix of plots for various values of p and q

library(tidyverse)
theme_set(theme_classic())

# Function to plot
my_fun = function(x, p, q) {
  1/(1 + exp(p + q*x))
}

# Parameters
params = expand.grid(p=seq(-2,2,length=6), q=seq(-1,1,length=11))

# x-values to feed to my_fun
x = seq(-10,10,0.1)

# Generate data frame for plotting
dat = map2_df(params$p, params$q, function(p, q) {
  data.frame(p=p, q=q, x, y=my_fun(x, p, q))
})

ggplot(dat, aes(x,y,colour=p, group=p)) +
  geom_line() +
  facet_grid(. ~ q, labeller=label_both) +
  labs(colour="p") +
  scale_colour_gradient(low="red", high="blue") +
  theme(legend.position="bottom")

enter image description here

3D plot with one variable fixed

The code below will produce an interactive 3D plot that you can zoom and rotate. I've fixed the value of p and drawn a plot of the y surface for a grid of x and q values.

library(rgl)

x = seq(-10,10,0.1)
q = seq(-1,1,0.01)
y = outer(x, q, function(a, b) 1/(1 + exp(1 + b*a)))

persp3d(x, q, y, col=hcl(240,80,65), specular="grey20",
        xlab = "x", ylab = "q", zlab = "y")

I'm not sure how to embed the interactive plot, but here's a static image of one viewing angle:

enter image description here

Shiny app

The code below will create the same plot as above, but with the added ability to vary p with a slider and see how the plot changes.

Open an R script file and paste in the code below. Save it as app.r in its own directory then run the code. Both an rgl window and the Shiny app page with the slider for controlling the value of p should open. Resize the windows as desired and then move the slider to see how the function surface changes for various values of p.

library(shiny)

# Define UI for application that draws an interactive plot
ui <- fluidPage(

   # Application title
   titlePanel("Plot the function 1/(1 + exp(p + q*x))"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         sliderInput("p",
                     "Vary the value of p and see how the plot changes",
                     min = -2,
                     max = 2,
                     value = 1,
                     step=0.2)
      ),

      # Show a plot of the generated distribution
      mainPanel(
         plotOutput("distPlot")
      )
   )
)

# Define server logic required to draw the plot
server <- function(input, output) {

   output$distPlot <- renderPlot({

     library(rgl)

     x = seq(-10,10,0.1)
     q = seq(-1,1,0.01)
     y = outer(x, q, function(a, b) 1/(1 + exp(input$p + b*a)))

     persp3d(x, q, y, col=hcl(240,50,65), specular="grey20",
             xlab = "x", ylab = "q", zlab = "y")

   })
}

# Run the application 
shinyApp(ui = ui, server = server)