gpt4 book ai didi

r - ggplot2 binwidth 在 facet_wrap 直方图中没有响应

转载 作者:行者123 更新时间:2023-12-04 12:34:42 27 4
gpt4 key购买 nike

感谢您对如何使我的代码或 binwidth 表现出任何想法。我的数据集包含每隔几个小时“4”和每天“24”收集的带时间戳的数据点。我试图在左侧绘制 4 小时堆叠直方图,在右侧绘制 24 小时堆叠直方图。因此,我希望右侧的 binwidth 比左侧的 binwidth 宽 6 倍。但是我用 binwidth 尝试过的一切都没有奏效。 x 轴数据 data3$dts 似乎是连续的而不是离散的,但也许我做得不对。

关于数据的重要说明:在右侧绘制的数据,即小时数=24 数据,具有始终为整数的 dts 值。左侧的数据,即 hours=4 数据,具有非整数 dts 值。

             "dts"  "Yes" "No" "Maybe" "hours"  "days"
"258" 15627.668 8 0 1 4 "7 Days"
"259" 15627.832 13 11 18 4 "7 Days"
"260" 15628 34 47 89 4 "7 Days"
"261" 15628 37 47 90 24 "7 Days"
"262" 15628.168 3 0 1 4 "7 Days"
"40" 15571 345 419 674 24 "90 Days"
"41" 15571.5 91 145 130 4 "90 Days"
"42" 15571.668 158 149 284 4 "90 Days"
"43" 15571.832 96 125 260 4 "90 Days"
"44" 15572 55 33 137 4 "90 Days"
"45" 15572 1050 1119 2660 24 "90 Days"

从 pastebin 中提取数据的代码:
library (ggplot2)
library (scales)
library(grid)
library(gridExtra)

color3 <- c("mediumspringgreen","red","grey44")
titles.days <- c( "7 Days", "90 Days")
names.facetby <- c ("dts", "hours", "days")

data3 <- read.table ("http://pastebin.com/download.php?i=wUQQUXP4", header=TRUE)
data3.melt <- melt (data3 , id = names.facetby )
data3.melt$days <- factor (data3.melt$days, levels = titles.days) # put the factor in the right order, so the graphs are in the right order

a <- ggplot ( data3.melt
, aes ( x = dts #as.Date( dts , date1970)
, y = value
, fill = variable)) +
opts (axis.text.x=theme_text(angle=0, hjust=1)) +
scale_fill_manual(values = color3) +
scale_x_date(labels = date_format("%m/%d\n %a") ) +
geom_histogram (stat = "identity", position = "stack", binwidth=6.2) +
facet_wrap( days ~ hours, ncol=2, scales="free")

print(a)

当前结果,显示了 binwidth 太窄的右侧图:

enter image description here

最佳答案

@贾斯汀的 link to Hadley Wickham's post有答案,那就是在不同的层中绘制左右图。

更新了在 ggplot 中使用 2 个新 geom_histogram 线正确绘制的代码:

图书馆(ggplot2)
图书馆(秤)
图书馆(网格)
图书馆(gridExtra)

color3 <- c("mediumspringgreen","red","grey44")
titles.days <- c( "7 Days", "90 Days")
names.facetby <- c ("dts", "hours", "days")

data3 <- read.table ("http://pastebin.com/download.php?i=wUQQUXP4", header=TRUE)
data3.melt <- melt (data3 , id = names.facetby )
data3.melt$days <- factor (data3.melt$days, levels = titles.days) # put the factor in the right order, so the graphs are in the right order



a <- ggplot ( data3.melt
, aes ( x = dts #as.Date( dts , date1970)
, y = value
, fill = variable)) +
opts (axis.text.x=theme_text(angle=0, hjust=1)) +
scale_fill_manual(values = color3) +
scale_x_date(labels = date_format("%m/%d\n%a") ) +

# bad idea, good ideas follow geom_histogram (stat = "identity", position = "stack", binwidth=6.2) + #, breaks = breaks.x
geom_histogram (data = subset(data3.melt, hours == 4), stat = "identity", position = "stack", binwidth=0.3) + #, breaks = breaks.x
geom_histogram (data = subset(data3.melt, hours == 24), stat = "identity", position = "stack", binwidth=0.9) + #, breaks = breaks.x

facet_wrap( days ~ hours, ncol=2, scales="free")

print(a) # plot the thing

修正图:
http://imgur.com/9j1Xz

关于r - ggplot2 binwidth 在 facet_wrap 直方图中没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12965170/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com