This is my goal: Plot the average of z according to bins formed by x and y in R










0














So I came across this answer here, How to make 3D histogram in R ,



and my question is, if I have three variable and I want to use the x and y to create bins, like using cut and table in the other answer, how can I then graph the z as the average of all the variable Z data that falls into those bins?



This what I have



library(plot3D)

x <- data$OPEXMKUP_PT_1d
y <- data$prod_opex


z <- data$ab90_ROIC_wogw3

x_c <- cut(x, 20)
y_c <- cut(y, 20)
cutup <- table(x_c, y_c)
mat <- data.frame(cutup)


hist3D(z = cutup, border="black", bty ="g",
main = "Data", xlab = "Markup",
ylab ="Omega", zlab = "Star")


But it show the z as the frequency, and when I try,



hist3D(x, y, z, phi = 0, bty = "g", type = "h", main = 'NEWer',
ticktype = "detailed", pch = 19, cex = 0.5,
xlim=c(0,3),
ylim=c(-10,20),
zlim=c(0,1))


It thinks for a long time and throws an error,



Error: protect(): protection stack overflow
Graphics error: Plot rendering error


It will do the 3dscatter fine but the data doesn't make sense since the Z variable is a ratio that falls mostly between 0 and 1, so you get a bunch of tall lines and and a bunch of short lines. I would like them averaged by bin to show a visual of how the average ratio changes as x and y change. Please let me know if there is a way to do this. Thanks!










share|improve this question























  • It will be helpful if you gave a sample of the data, or show how to simulate data similar to what you're working with.
    – mickey
    Nov 13 '18 at 0:19















0














So I came across this answer here, How to make 3D histogram in R ,



and my question is, if I have three variable and I want to use the x and y to create bins, like using cut and table in the other answer, how can I then graph the z as the average of all the variable Z data that falls into those bins?



This what I have



library(plot3D)

x <- data$OPEXMKUP_PT_1d
y <- data$prod_opex


z <- data$ab90_ROIC_wogw3

x_c <- cut(x, 20)
y_c <- cut(y, 20)
cutup <- table(x_c, y_c)
mat <- data.frame(cutup)


hist3D(z = cutup, border="black", bty ="g",
main = "Data", xlab = "Markup",
ylab ="Omega", zlab = "Star")


But it show the z as the frequency, and when I try,



hist3D(x, y, z, phi = 0, bty = "g", type = "h", main = 'NEWer',
ticktype = "detailed", pch = 19, cex = 0.5,
xlim=c(0,3),
ylim=c(-10,20),
zlim=c(0,1))


It thinks for a long time and throws an error,



Error: protect(): protection stack overflow
Graphics error: Plot rendering error


It will do the 3dscatter fine but the data doesn't make sense since the Z variable is a ratio that falls mostly between 0 and 1, so you get a bunch of tall lines and and a bunch of short lines. I would like them averaged by bin to show a visual of how the average ratio changes as x and y change. Please let me know if there is a way to do this. Thanks!










share|improve this question























  • It will be helpful if you gave a sample of the data, or show how to simulate data similar to what you're working with.
    – mickey
    Nov 13 '18 at 0:19













0












0








0







So I came across this answer here, How to make 3D histogram in R ,



and my question is, if I have three variable and I want to use the x and y to create bins, like using cut and table in the other answer, how can I then graph the z as the average of all the variable Z data that falls into those bins?



This what I have



library(plot3D)

x <- data$OPEXMKUP_PT_1d
y <- data$prod_opex


z <- data$ab90_ROIC_wogw3

x_c <- cut(x, 20)
y_c <- cut(y, 20)
cutup <- table(x_c, y_c)
mat <- data.frame(cutup)


hist3D(z = cutup, border="black", bty ="g",
main = "Data", xlab = "Markup",
ylab ="Omega", zlab = "Star")


But it show the z as the frequency, and when I try,



hist3D(x, y, z, phi = 0, bty = "g", type = "h", main = 'NEWer',
ticktype = "detailed", pch = 19, cex = 0.5,
xlim=c(0,3),
ylim=c(-10,20),
zlim=c(0,1))


It thinks for a long time and throws an error,



Error: protect(): protection stack overflow
Graphics error: Plot rendering error


It will do the 3dscatter fine but the data doesn't make sense since the Z variable is a ratio that falls mostly between 0 and 1, so you get a bunch of tall lines and and a bunch of short lines. I would like them averaged by bin to show a visual of how the average ratio changes as x and y change. Please let me know if there is a way to do this. Thanks!










share|improve this question















So I came across this answer here, How to make 3D histogram in R ,



and my question is, if I have three variable and I want to use the x and y to create bins, like using cut and table in the other answer, how can I then graph the z as the average of all the variable Z data that falls into those bins?



This what I have



library(plot3D)

x <- data$OPEXMKUP_PT_1d
y <- data$prod_opex


z <- data$ab90_ROIC_wogw3

x_c <- cut(x, 20)
y_c <- cut(y, 20)
cutup <- table(x_c, y_c)
mat <- data.frame(cutup)


hist3D(z = cutup, border="black", bty ="g",
main = "Data", xlab = "Markup",
ylab ="Omega", zlab = "Star")


But it show the z as the frequency, and when I try,



hist3D(x, y, z, phi = 0, bty = "g", type = "h", main = 'NEWer',
ticktype = "detailed", pch = 19, cex = 0.5,
xlim=c(0,3),
ylim=c(-10,20),
zlim=c(0,1))


It thinks for a long time and throws an error,



Error: protect(): protection stack overflow
Graphics error: Plot rendering error


It will do the 3dscatter fine but the data doesn't make sense since the Z variable is a ratio that falls mostly between 0 and 1, so you get a bunch of tall lines and and a bunch of short lines. I would like them averaged by bin to show a visual of how the average ratio changes as x and y change. Please let me know if there is a way to do this. Thanks!







r 3d histogram






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 23:54

























asked Nov 12 '18 at 23:32









Nick Hosler

34




34











  • It will be helpful if you gave a sample of the data, or show how to simulate data similar to what you're working with.
    – mickey
    Nov 13 '18 at 0:19
















  • It will be helpful if you gave a sample of the data, or show how to simulate data similar to what you're working with.
    – mickey
    Nov 13 '18 at 0:19















It will be helpful if you gave a sample of the data, or show how to simulate data similar to what you're working with.
– mickey
Nov 13 '18 at 0:19




It will be helpful if you gave a sample of the data, or show how to simulate data similar to what you're working with.
– mickey
Nov 13 '18 at 0:19












1 Answer
1






active

oldest

votes


















0














Not sure exactly what your data looks like, so I made some up. You should be able to adjust to your needs. It's a bit hacky/brute force-ish, but could work just fine if your data isn't too large to slow down the loop.



library(plot3D)

# Fake it til you make it
n = 5000
x = runif(n)
y = runif(n)
z = x + 2*y + sin(x*2*pi)

# Divide into bins
x_c = cut(x, 20)
y_c = cut(y, 20)
x_l = levels(x_c)
y_l = levels(y_c)

# Compute the mean of z within each x,y bin
z_p = matrix(0, 20, 20)
for (i in 1:length(x_l))
for (j in 1:length(y_l))
z_p[i,j] = mean(z[x_c %in% x_l[i] & y_c %in% y_l[j]])



# Get the middle of each bin
x_p = sapply(strsplit(gsub('\(|]', '', x_l), ','), function(x) mean(as.numeric(x)))
y_p = sapply(strsplit(gsub('\(|]', '', y_l), ','), function(x) mean(as.numeric(x)))

# Plot
hist3D(x_p, y_p, z_p, bty = "g", type = "h", main = 'NEWer',
ticktype = "detailed", pch = 19, cex = 0.5)


Basically, we're just manually computing the average bin height z by looping over the bins. There may be a better way to do the computation.



enter image description here






share|improve this answer




















  • Worked great! Thank you so much!
    – Nick Hosler
    Nov 13 '18 at 5:21










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271631%2fthis-is-my-goal-plot-the-average-of-z-according-to-bins-formed-by-x-and-y-in-r%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Not sure exactly what your data looks like, so I made some up. You should be able to adjust to your needs. It's a bit hacky/brute force-ish, but could work just fine if your data isn't too large to slow down the loop.



library(plot3D)

# Fake it til you make it
n = 5000
x = runif(n)
y = runif(n)
z = x + 2*y + sin(x*2*pi)

# Divide into bins
x_c = cut(x, 20)
y_c = cut(y, 20)
x_l = levels(x_c)
y_l = levels(y_c)

# Compute the mean of z within each x,y bin
z_p = matrix(0, 20, 20)
for (i in 1:length(x_l))
for (j in 1:length(y_l))
z_p[i,j] = mean(z[x_c %in% x_l[i] & y_c %in% y_l[j]])



# Get the middle of each bin
x_p = sapply(strsplit(gsub('\(|]', '', x_l), ','), function(x) mean(as.numeric(x)))
y_p = sapply(strsplit(gsub('\(|]', '', y_l), ','), function(x) mean(as.numeric(x)))

# Plot
hist3D(x_p, y_p, z_p, bty = "g", type = "h", main = 'NEWer',
ticktype = "detailed", pch = 19, cex = 0.5)


Basically, we're just manually computing the average bin height z by looping over the bins. There may be a better way to do the computation.



enter image description here






share|improve this answer




















  • Worked great! Thank you so much!
    – Nick Hosler
    Nov 13 '18 at 5:21















0














Not sure exactly what your data looks like, so I made some up. You should be able to adjust to your needs. It's a bit hacky/brute force-ish, but could work just fine if your data isn't too large to slow down the loop.



library(plot3D)

# Fake it til you make it
n = 5000
x = runif(n)
y = runif(n)
z = x + 2*y + sin(x*2*pi)

# Divide into bins
x_c = cut(x, 20)
y_c = cut(y, 20)
x_l = levels(x_c)
y_l = levels(y_c)

# Compute the mean of z within each x,y bin
z_p = matrix(0, 20, 20)
for (i in 1:length(x_l))
for (j in 1:length(y_l))
z_p[i,j] = mean(z[x_c %in% x_l[i] & y_c %in% y_l[j]])



# Get the middle of each bin
x_p = sapply(strsplit(gsub('\(|]', '', x_l), ','), function(x) mean(as.numeric(x)))
y_p = sapply(strsplit(gsub('\(|]', '', y_l), ','), function(x) mean(as.numeric(x)))

# Plot
hist3D(x_p, y_p, z_p, bty = "g", type = "h", main = 'NEWer',
ticktype = "detailed", pch = 19, cex = 0.5)


Basically, we're just manually computing the average bin height z by looping over the bins. There may be a better way to do the computation.



enter image description here






share|improve this answer




















  • Worked great! Thank you so much!
    – Nick Hosler
    Nov 13 '18 at 5:21













0












0








0






Not sure exactly what your data looks like, so I made some up. You should be able to adjust to your needs. It's a bit hacky/brute force-ish, but could work just fine if your data isn't too large to slow down the loop.



library(plot3D)

# Fake it til you make it
n = 5000
x = runif(n)
y = runif(n)
z = x + 2*y + sin(x*2*pi)

# Divide into bins
x_c = cut(x, 20)
y_c = cut(y, 20)
x_l = levels(x_c)
y_l = levels(y_c)

# Compute the mean of z within each x,y bin
z_p = matrix(0, 20, 20)
for (i in 1:length(x_l))
for (j in 1:length(y_l))
z_p[i,j] = mean(z[x_c %in% x_l[i] & y_c %in% y_l[j]])



# Get the middle of each bin
x_p = sapply(strsplit(gsub('\(|]', '', x_l), ','), function(x) mean(as.numeric(x)))
y_p = sapply(strsplit(gsub('\(|]', '', y_l), ','), function(x) mean(as.numeric(x)))

# Plot
hist3D(x_p, y_p, z_p, bty = "g", type = "h", main = 'NEWer',
ticktype = "detailed", pch = 19, cex = 0.5)


Basically, we're just manually computing the average bin height z by looping over the bins. There may be a better way to do the computation.



enter image description here






share|improve this answer












Not sure exactly what your data looks like, so I made some up. You should be able to adjust to your needs. It's a bit hacky/brute force-ish, but could work just fine if your data isn't too large to slow down the loop.



library(plot3D)

# Fake it til you make it
n = 5000
x = runif(n)
y = runif(n)
z = x + 2*y + sin(x*2*pi)

# Divide into bins
x_c = cut(x, 20)
y_c = cut(y, 20)
x_l = levels(x_c)
y_l = levels(y_c)

# Compute the mean of z within each x,y bin
z_p = matrix(0, 20, 20)
for (i in 1:length(x_l))
for (j in 1:length(y_l))
z_p[i,j] = mean(z[x_c %in% x_l[i] & y_c %in% y_l[j]])



# Get the middle of each bin
x_p = sapply(strsplit(gsub('\(|]', '', x_l), ','), function(x) mean(as.numeric(x)))
y_p = sapply(strsplit(gsub('\(|]', '', y_l), ','), function(x) mean(as.numeric(x)))

# Plot
hist3D(x_p, y_p, z_p, bty = "g", type = "h", main = 'NEWer',
ticktype = "detailed", pch = 19, cex = 0.5)


Basically, we're just manually computing the average bin height z by looping over the bins. There may be a better way to do the computation.



enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 0:47









mickey

1,2181216




1,2181216











  • Worked great! Thank you so much!
    – Nick Hosler
    Nov 13 '18 at 5:21
















  • Worked great! Thank you so much!
    – Nick Hosler
    Nov 13 '18 at 5:21















Worked great! Thank you so much!
– Nick Hosler
Nov 13 '18 at 5:21




Worked great! Thank you so much!
– Nick Hosler
Nov 13 '18 at 5:21

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271631%2fthis-is-my-goal-plot-the-average-of-z-according-to-bins-formed-by-x-and-y-in-r%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党