关于如何在geom_bin2d上标注计数,可以在这里找到一个很好的答案:
但是,当将其修改为具有对数X轴时:
library(ggplot2)
set.seed(1)
dat <- data.frame(x = rnorm(1000), y = rnorm(1000))
# plot MODIFIED HERE TO BECOME log10
p <- ggplot(dat, aes(x = x, y = y)) + geom_bin2d() + scale_x_log10()
# Get data - this includes counts and x,y coordi
我只是在尝试用R/GGPLOT2 2创建可视化。我正在尝试的图表是一个浮动条形图(一个使条形图从一个最小值变为一个最大值)。然而,叠加在这上面,我想要一个趋势线,是在一个第二轴。到目前为止,这是我的尝试:
# first, your data
table1 <- read.table(text = 'X A B C D E F G H I J K L
1 "BAR TOP" 31.5 31.8 30.3 28.0 24.9 24.4 21.7 20.9 24.5 25.4 26.0 28.7
我使用ggplot2和ggmics生成趋势线。
library(ggplot2)
library(ggpmisc)
library(scales)
x <- c(5,2,6,8,9,1,3,6,8,2)
y <- c(4,7,2,5,7,9,5,2,1,3)
df <- data.frame(x,y)
g <- ggplot(df,aes(x,y))
g <- g + geom_point(colour = "black")
g <- g + stat_smooth(method = lm, formula = y ~ x, se = F
当我向图中添加一个次轴时,变换公式发生了一些奇怪的事情。我很困惑,所以我创建了一个非常简单的reprex。我希望~ -.公式只显示负数,但这不起作用。请帮助:) library(tibble)
library(ggplot2)
dat <- tibble(x = 1:10, y = 11:20)
pl <- ggplot(dat, aes(x = x, y = y)) +
geom_point()
# add a secondary "negative" axis as an example
pl +
scale_y_continuous("
我有一个DF
a b
A 1
B 1
C 1
B 1
B 1
A 1
我试图用ggplot绘制条形图,DF$a在x轴上,DF$b在y轴上。我使用以下方法
p <- ggplot(DF, aes(a,b)) + geom_bar(stat="identity")
但看来ggplot只画出了来自DF$a的唯一字母,而我想画出所有这些字母。我该怎么做呢?谢谢你的帮助。
假设我有以下虚拟数据框架:
df <- data.frame(let = LETTERS[1:13], value = sample(13),
group = rep(c("foo", "bar"), times = c(5,8)))
df
let value group
1 A 2 foo
2 B 1 foo
3 C 12 foo
4 D 8 foo
5 E 4 foo
6 F 13 bar
7 G 11 bar
8 H
在这个问题之后:,我也想在ggplot boxplot中添加每个组的观察值数量。但是我已经在aes映射中添加了一种颜色。
现有的答案显示了如何调整文本在y轴上的位置。如何调整文本在x轴上的位置?
这是重现我的问题的最小示例:
library(ggplot2)
give.n <- function(x){
return(c(y = median(x)*1.05, label = length(x)))
# experiment with the multiplier to find the perfect position
}
p <- ggplot(mtcars,