简体   繁体   中英

Why does my histogram does not have negative value on x axis?

I am plotting a histogram that shows the distribution of a variable. The variable plotted on the x-axis contains negative values but on the histogram, those values are not present.

Here is a code to reproduce a sample of the dataset:

structure(list(`Cash Flowth EURLast avail. yr` = c(2.355, 14.677, 
-7.923, 53.66, 0, 91.336, 111.12, 11.945, -0.069, 4.42, 58.943, 
14.687, 11.17, 32.825, -1432.259, 2.852, 34.489, 198.515, 77.64, 
1.195, -53.123, -24.501, 18.244, 18.438, 16.668, 343.301, 0, 
-32.001, 41.009, -3.509, 71.679, 33.581, 638.27, 0, -1.262, -0.853, 
380.624, 26.533, 1.65, -30.007, -709.602, 1.877, -0.498, 3.77, 
-27.749, 15.599, -69.519, 6.331, 0.277, -150.365), general_status = c("Failed", 
"Active", "Failed", "Active", "Failed", "Active", "Active", "Active", 
"Failed", "Active", "Active", "Active", "Active", "Active", "Failed", 
"Active", "Active", "Active", "Active", "Failed", "Failed", "Active", 
"Active", "Active", "Failed", "Active", "Failed", "Failed", "Active", 
"Active", "Active", "Active", "Active", "Failed", "Active", "Failed", 
"Active", "Active", "Active", "Failed", "Failed", "Active", "Active", 
"Active", "Active", "Failed", "Active", "Active", "Failed", "Failed"
)), row.names = c(NA, -50L), class = c("tbl_df", "tbl", "data.frame"
))

Here is my code for plot the histogram:

df %>%
  filter(!is.na(`Cash Flowth EURLast avail. yr`)) %>%
  ggplot(aes(x = `Cash Flowth EURLast avail. yr`, fill = as.factor(general_status))) +
  geom_histogram(
      bins = nclass.Sturges(`Cash Flowth EURLast avail. yr`),colour = "black", position="identity")+
  scale_fill_manual(values = c("Active" = "springgreen4", "Failed" = "firebrick3"))+
  theme(legend.position="None", strip.background = element_rect(colour="black",
                                        fill="white"))+
  facet_grid(~general_status)

在此处输入图片说明

How can I fix this problem? knowing that the min = -901535 and max = 8009206

This may not be qualified as an answer, but it is difficult to explain as a comment.

Your variable range is

range(df$`Cash Flowth EURLast avail. yr`)
[1] -1432.259   638.270

When x-axis range is too high, you can't see negative values which are actually there. You may not specify xlim(-1500, 650) to fix this.

Also, you code does not work on my computer. I replaced bins = nclass.Sturges(Cash Flowth EURLast avail. yr )`

df %>%
  filter(!is.na(`Cash Flowth EURLast avail. yr`)) %>%
  ggplot(aes(x = `Cash Flowth EURLast avail. yr`, fill = as.factor(general_status))) +
  geom_histogram(
    bins = nclass.Sturges(df$`Cash Flowth EURLast avail. yr`), colour = "black", position="identity")+
  scale_fill_manual(values = c("Active" = "springgreen4", "Failed" = "firebrick3"))+
  theme(legend.position="None", strip.background = element_rect(colour="black",
                                                                fill="white"))+
  facet_grid(~general_status)

在此处输入图片说明

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