- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问。对于绘制的三个不同变量中的一个,我如何防止值 < 0 显示在 ggplot2 方面图中,每个变量都有不同的大小?
我制作了以下方面图。
如您所见,绘制在 y 轴上的值对于每个变量都明显不同,但使用 scales = "free"
可以解决该问题。
我想通过限制比例或将小于零的值设置为零来抑制“profit_margin”方面(底部为蓝色)中小于零的值,但我不知道如何完成此操作.我可以直接改变数据框中的值,但我更愿意保持数据不变。我尝试在 scale_y_continuous() 中使用一个函数,但无法取得任何进展。
这是用于生成上述图的代码:
require(lubridate)
require(reshape2)
require(ggplot2)
require(scales)
## Create dummy time series data
set.seed(12345)
monthsback <- 12
startdate <- as.Date(paste(year(now()),month(now()),"1",sep = "-")) - months(monthsback)
mydf <- data.frame(mydate = seq(as.Date(startdate), by = "month", length.out = monthsback),
sales = runif(monthsback, min = 600, max = 800),
profit = runif(monthsback, min = -50, max = 80))
## Add calculation based on data
mydf$profit_margin <- mydf$profit/mydf$sales
## Reshape...
mymelt <- melt(mydf, id = c('mydate'))
## Plot
p <- ggplot(data = mymelt, aes(x = mydate, y = value, fill = variable)) +
geom_bar(stat = "identity") +
facet_wrap( ~ variable, ncol = 1, scales = "free")
print(p)
这是我尝试使用一个函数并 lapply 将低于零的值设置为零:
require(lubridate)
require(reshape2)
require(ggplot2)
require(scales)
## Create dummy time series data
set.seed(12345)
monthsback <- 12
startdate <- as.Date(paste(year(now()),month(now()),"1",sep = "-")) - months(monthsback)
mydf <- data.frame(mydate = seq(as.Date(startdate), by = "month", length.out = monthsback),
sales = runif(monthsback, min = 600, max = 800),
profit = runif(monthsback, min = -50, max = 80))
## Add calculation based on data
mydf$profit_margin <- mydf$profit/mydf$sales
## Reshape...
mymelt <- melt(mydf, id = c('mydate'))
scales_function <- function(myvar, myvalue){
mycount <- 1
newval <- lapply(myvalue, function(myarg) {
myarg <- ifelse(myvar[mycount] == "profit_margin", ifelse(myarg < 0, 0, myarg), myarg)
}
)
return(newval)
}
## Plot
p <- ggplot(data = mymelt, aes(x = mydate, y = value, fill = variable)) +
geom_bar(stat = "identity") +
facet_wrap( ~ variable, ncol = 1, scales = "free") +
scale_y_continuous(breaks = scales_function(mymelt$variable, mymelt$value))
print(p)
最佳答案
您可以保持数据不变,但只绘制一个子集。
ggplot(data = subset(mymelt,!((variable == 'profit_margin') & value < 0)),
aes(x = mydate, y = value, fill = variable)) +
geom_bar(stat = "identity") +
facet_wrap( ~ variable, ncol = 1, scales = "free")
或者在调用中替换
ggplot(data = mymelt, aes(x = mydate, y = replace(value, (variable == 'profit_margin') & value <0, NA), fill = variable)) +
geom_bar(stat = "identity") +
facet_wrap( ~ variable, ncol = 1, scales = "free") +
ylab('value')
关于R:如何有条件地更改 ggplot 构面图中使用的 3 个变量中的 1 个变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10809157/
我开始考虑在我 future 的项目或重构中实现控制反转容器,我想知道在正确设计依赖项时哪些原则(除了 GoF 模式)可能需要牢记在心。假设我需要构建一个简单的控制台应用程序,如果它可以访问互联网,它
假设我有一个 RxC contingency table 。这意味着有 R 行和 C 列。我想要一个维度为 RC × (R + C − 2) 的矩阵 X,其中包含行的 R − 1 “主效应”以及列的
我正在尝试使用 DKMS 为正在运行的内核 (4.4) 构 build 备树覆盖。我天真的 Makefile 如下: PWD := $(shell pwd) dtbo-y += my-awsome-o
我有一个 sencha touch 项目。我是用 phonegap 2.9 构建的,并且可以正常工作 device.uuid 返回到设备 ID。当我尝试使用 3.1 device.uuid 构建时抛出
我在安装了 Xcode 4.5.1 的 Mt Lion 上运行。 默认情况下,当我构建并部署到 iOS 5.1 设备时,显示会在我旋转设备时旋转,但当我部署到 iOS 6 模拟器或运行 iOS 的 i
我正在尝试使用 Google Analytics Reporting API v4 构建多折线图。 一张图表,其中我按每天的 session 计数为每个设备(台式机/平板电脑/移动设备)设置了一条线。
我一生都无法使用 xcode 组织者“自动设备配置”中的“团队配置配置文件”在 xcode 4.0.1 中将我的应用程序构建到我的 iPad 上。 该应用程序完美地构建到模拟器,但当我构建到 iPad
我是一名优秀的程序员,十分优秀!