试图理解binwidth为geom_histogram. 虽然到目前为止失败了。
这是可重现的数据
a <- rnorm(10,7,0.1)
b <- rnorm(10,13,5)
df <- data.frame(data = c(a,b),group=c(rep("a",10),rep("b",10)))
kk <- df%>%
group_by(group)%>%
mutate(bin=density(data)$bw)
binns <- round(unique(kk$bin),digits = 2) # to get each binwidth for each group
ggplot()+
geom_histogram(data=kk,aes(x=data, fill=group),binwidth=binss)+
facet_wrap(~group,scales=c("free_y"))
Error in seq.default(round_any(range[1], size, floor), round_any(range[2], :
'from' must be of length 1
Error in seq.default(round_any(range[1], size, floor), round_any(range[2], :
'from' must be of length 1
Error in exists(name, envir = env, mode = mode) :
argument "env" is missing, …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的问题,但到目前为止找不到简单的解决方案。假设我有一些数据想要拟合并显示其 x 轴值,其中 y 是特定值。在这种情况下,假设当 y=0 时 x 值是多少。模型非常简单 y~x 用于拟合,但我不知道如何从那里估计 x 值。反正,
样本数据
library(ggplot2)
library(scales)
df = data.frame(x= sort(10^runif(8,-6,1),decreasing=TRUE), y = seq(-4,4,length.out = 8))
ggplot(df, aes(x = x, y = y)) +
geom_point() +
#geom_smooth(method = "lm", formula = y ~ x, size = 1,linetype="dashed", col="black",se=FALSE, fullrange = TRUE)+
geom_smooth(se=FALSE)+
labs(title = "Made-up data") +
scale_x_log10(breaks = c(1e-6,1e-4,1e-2,1),
labels = trans_format("log10", math_format(10^.x)),limits = c(1e-6,1))+
geom_hline(yintercept=0,linetype="dashed",colour="red",size=0.6)
Run Code Online (Sandbox Code Playgroud)
我想将 1e-10 输入转换为 10^-10 格式并在绘图上对其进行注释。正如我在情节中指出的那样。
提前致谢!
我想生成一个facet_wrap,其中构面内因子的顺序基于列因子顺序之一。问题的核心是每个组都有重复的因子水平,当我绘制时,只有一个因子水平在facet_wrap. (见下图)
我尝试对每个组中的因子级别进行排序,并且每个因子级别应在每个方面内正确排序。
这是我的尝试
df_pattern<- data.frame(address = rep(rep(LETTERS[1:3]),3))
df_TP <- data.frame(No=rep(seq(1:3)),
clas=c("Good","Bad","Ugly"),stringsAsFactors = F)
set.seed(12)
df_ex <- df_pattern%>%
mutate(No=rep(seq(1:3),each=3))%>%
left_join(df_TP)%>%
mutate(clas=sample(clas))%>%
group_by(No)
# address No clas
# <fctr> <int> <chr>
# 1 A 1 Good
# 2 B 1 Ugly
# 3 C 1 Ugly
# 4 A 2 Good
# 5 B 2 Ugly
# 6 C 2 Bad
# 7 A 3 Bad
# 8 B 3 Bad
# 9 C 3 Good
Run Code Online (Sandbox Code Playgroud)
现在让我们尝试根据用户定义的类列顺序对地址级别进行排序
set.seed(12)
df_ex …Run Code Online (Sandbox Code Playgroud) 您好,我有一个数据,我想重命名其中一列并选择以t字符串开头的列。
raw_data = {'patient': [1, 1, 1, 2, 2],
'obs': [1, 2, 3, 1, 2],
'treatment': [0, 1, 0, 1, 0],
'score': ['strong', 'weak', 'normal', 'weak', 'strong'],
'tr': [1,2,3,4,5],
'tk': [6,7,8,9,10],
'ak': [11,12,13,14,15]
}
df = pd.DataFrame(raw_data, columns = ['patient', 'obs', 'treatment', 'score','tr','tk','ak'])
df
patient obs treatment score tr tk ak
0 1 1 0 strong 1 6 11
1 1 2 1 weak 2 7 12
2 1 3 0 normal 3 8 13
3 2 1 …Run Code Online (Sandbox Code Playgroud) 当我表演时:
a <- seq(1,1.5,0.1)
b <- c(1,1.1,1.4,1.5)
x <- rep(c(a,b),times=c(2,1))
Run Code Online (Sandbox Code Playgroud)
rep(c(a,b),c(2,1))出错:无效的'times'参数
为什么?
我有一个关于在data.frame中对组进行编号的问题.
我在这里找到了一个类似的方法dplyr-how-to-number-label-data-table-by-group-number-from-group-by
但它对我没有用.我不知道为什么.
S <- rep(letters[1:12],each=6)
R = sort(replicate(9, sample(5000:6000,4)))
df <- data.frame(R,S)
get_next_integer = function(){
i = 0
function(S){ i <<- i+1 }
}
get_integer = get_next_integer()
result <- df %>% group_by(S) %>% mutate(label = get_integer())
result
Source: local data frame [72 x 3]
Groups: S [12]
R S label
(int) (fctr) (dbl)
1 5058 a 1
2 5121 a 1
3 5129 a 1
4 5143 a 1
5 5202 a 1
6 5213 a 1
7 …Run Code Online (Sandbox Code Playgroud) 我只是想计算我的x,y,z数据帧与参考向量之间的相对角度.到目前为止,我dplyr用来分组并运用我的angle函数来获得相对角度.然而,即使我在这里提供的虚拟数据,事情也很慢.
set.seed(12345)
x <- replicate(1,c(replicate(1000,rnorm(50,0,0.01))))
y <- replicate(1,c(replicate(1000,rnorm(50,0,0.01))))
z <- replicate(1,c(replicate(1000,rnorm(50,0.9,0.01))))
ref_vector <- data.frame(ref_x=rep(0,100),ref_y=rep(0,100),ref_z=rep(1,100))
set <- rep(seq(1,1000),each=50)
data_rep <- data.frame(x,y,z,ref_vector,set)
Run Code Online (Sandbox Code Playgroud)
>
head(data_rep)
# x y z ref_x ref_y ref_z set
# 1 0.005855288 -0.015472796 0.9059337 0 0 1 1
# 2 0.007094660 -0.013354359 0.9040137 0 0 1 1
# 3 -0.001093033 -0.014661486 0.9047502 0 0 1 1
# 4 -0.004534972 -0.002764655 0.9070553 0 0 1 1
# 5 0.006058875 -0.008339952 0.8926551 0 0 1 1
# …Run Code Online (Sandbox Code Playgroud) 在文本中插入数学符号应该不是那么复杂我想!
OTH,甚至用数学表达式查看类似的例子 ggplot2 facet_wrap
我仍然无法插入Ω(Omega)符号geom_text!
假设您有基本的散点图,并且您希望将(Omega)数学符号的平均值添加到每个方面,
mean.Petal <- aggregate(iris["Petal.Width"], iris["Species"], mean)
Species Petal.Width
1 setosa 0.246
2 versicolor 1.326
3 virginica 2.026
ggplot(iris) +
geom_point(aes(y=Sepal.Length,x=Sepal.Width ,col=factor(Species))) +
facet_wrap(~ Species)+
geom_text(data = mean.Petal, parse = TRUE,
aes(x = 4.5, y = 7, label=sprintf('mean_Petal=%.2f %s',
round(Petal.Width,digits=2),'Omega')))
Run Code Online (Sandbox Code Playgroud)
解析时出错(text = as.character(lab)):: 1:17:意外符号1:mean_Petal = 0.25欧米茄
另一个尝试
geom_text(data = mean.Petal, parse = TRUE,
aes(x = 4.5, y = 7, label=paste('mean_Petal=',
round(Petal.Width,digits=2),expression(Omega),sep=' ')))
Run Code Online (Sandbox Code Playgroud)
解析时出错(text = as.character(lab))::: 1:18:意外符号1:mean_Petal = 0.25欧米茄
我有以下字符串。我试图删除最后一个空格之前的所有字符串,但似乎无法实现。
我试着关注这个帖子
str <- c("Veni vidi vici")
gsub("\\s*","\\1",str)
"Venividivici"
Run Code Online (Sandbox Code Playgroud)
我想要的是"vici"在删除最后一个空格之前的所有内容后只剩下字符串。
我环顾四周,但找不到解决方案。在R's dplyr我们可以选择在一行代码命名列。
select(Com=Commander,Sco=Score)
Run Code Online (Sandbox Code Playgroud)
我正在尝试在熊猫中做同样的事情,但还没有找到可行的解决方案!
假设我们有这个样本数据
# Create an example dataframe
data = {'Commander': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
'Date': ['2012, 02, 08', '2012, 02, 08', '2012, 02, 08', '2012, 02, 08', '2012, 02, 08'],
'Score': [4, 24, 31, 2, 3]}
df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])
df
Commander Date Score
Cochice Jason 2012, 02, 08 4
Pima Molly 2012, 02, 08 24
Santa Cruz Tina 2012, 02, 08 31
Maricopa Jake 2012, 02, …Run Code Online (Sandbox Code Playgroud)