我有一个数据框:
df1 <- data.frame(place = c("a", "a", "b", "b", "c", "c", "d", "d", "e", "e", "f", "f", "g", "g",
"h", "h", "i", "i", "j", "j", "k", "k", "l", "l", "m", "m", "n",
"n", "o", "o", "p", "p", "q", "q"),
cost_other = c("cost_savings", "other_savings",
"cost_savings", "other_savings",
"cost_savings", "other_savings",
"cost_savings", "other_savings",
"cost_savings", "other_savings",
"cost_savings", "other_savings",
"cost_savings", "other_savings",
"cost_savings", "other_savings",
"cost_savings", "other_savings",
"cost_savings", "other_savings",
"cost_savings", "other_savings",
"cost_savings", "other_savings",
"cost_savings", "other_savings",
"cost_savings", "other_savings",
"cost_savings", "other_savings",
"cost_savings", "other_savings",
"cost_savings", "other_savings"),
values = c(1.8,0.55, 0.836, 1.06, …Run Code Online (Sandbox Code Playgroud) 我有一个数据框,它的列名由 R 分配。
Col1 Col2 Col3
NA NA ABC
NA NA XYZ
Name1 Name2 Name3
Joe Paul Ross
Run Code Online (Sandbox Code Playgroud)
我想删除前 3 行,包括由 R 分配的 Col Numbers 标题,并使 Name1、Name2、Name 3 成为数据框中列的标题名称。
删除行,我试图做 tail(df,-3).. 但它没有删除 Col Number 行,并删除我的 Name 行
我有一些数据,我想使用 nls 将非线性模型拟合到数据的每个子集,然后使用 ggplot2 将拟合模型叠加到数据点图上。具体来说,模型的形式是
y~V*x/(K+x)
Run Code Online (Sandbox Code Playgroud)
您可以将其识别为 Michaelis-Menten。一种方法是使用 geom_smooth,但如果我使用 geom_smooth,我将无法检索模型拟合的系数。或者,我可以使用 nls 拟合数据,然后使用 geom_smooth 绘制拟合线,但是我怎么知道 geom_smooth 绘制的曲线与我的 nls 拟合给出的曲线相同?我不能将我的 nls 拟合系数传递给 geom_smooth 并告诉它使用它们,除非我可以让 geom_smooth 只使用数据的一个子集,然后我可以指定起始参数,这样就可以了,但是......每个有一次我试过,读到的错误如下:
Aesthetics must be either length 1 or the same as the data (8): x, y, colour
Run Code Online (Sandbox Code Playgroud)
以下是我一直在使用的一些示例数据:
Concentration <- c(500.0,250.0,100.0,62.5,50.0,25.0,12.5,5.0,
500.0,250.0,100.0,62.5,50.0,25.0,12.5,5.0)
drug <- c(1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2)
rate <- c(1.889220,1.426500,0.864720,0.662210,0.564340,0.343140,0.181120,0.077170,
3.995055,3.011800,1.824505,1.397237,1.190078,0.723637,0.381865,0.162771)
file<-data.frame(Concentration,drug,rate)
Run Code Online (Sandbox Code Playgroud)
在我的图中,浓度为 x,速率为 y;药物将是颜色变量。如果我编写以下内容,则会出现该错误:
plot <- ggplot(file,aes(x=file[,1],y=file[,3],color=Compound))+geom_point()
plot<-plot+geom_smooth(data=subset(file,file[,2]==drugNames[i]),method.args=list(formula=y~Vmax*x/(Km+x),start=list(Vmax=coef(models[[i]])[1],Km=coef(models[[i]])[2])),se=FALSE,size=0.5)
Run Code Online (Sandbox Code Playgroud)
其中models[[]] 是nls 返回的模型参数列表。
关于如何在 geom_smooth 中对数据框进行子集化以便我可以使用 nls 拟合中的起始参数单独绘制曲线的任何想法?
我有以下清单:
mylist<-list(c("25","0"),c("50","1"),c("100","2"))
Run Code Online (Sandbox Code Playgroud)
我想立即提取列表中每个元素的第一个元素。那是
c(mylist[[1]][1],mylist[[2]][1],mylist[[3]][1])
Run Code Online (Sandbox Code Playgroud)
我尝试了以下但没有成功:
mylist[[]][1]; mylist[[.]][1]; mylist[1:3][1]
Run Code Online (Sandbox Code Playgroud)
我感谢任何有效地做到这一点的建议
下面的代码可以将圆环图绘制为附图中的左侧图表,如何将标签更改为右侧图表?谢谢!
library(tidyverse)
pie_data <- data.frame(category=c('A','B','C'),sales=c(70,25,40))
pie_data %>% ggplot(aes(x=1,y=sales,fill=category))+
geom_col()+
scale_fill_brewer(direction = -1)+
geom_text(position=position_stack(vjust=0.5),aes(label=paste0('Category ',category,':',sales)))+
xlim(-1,2)+
coord_polar(theta = 'y')+
theme_void()
Run Code Online (Sandbox Code Playgroud)
我有一个数据框(df),列出与每个站点关联的国家/地区
Site Country
Site1 USA
Site2 Vietnam
Site3 Spain
Site4 Germany
Site5 China
Run Code Online (Sandbox Code Playgroud)
我想附上一个专栏,我将每个国家与其相应的大陆联系起来.我写了一个简单if loop的做法:
df$Continent <- NA
if(df$Country == "USA" |df$Country == "Canada" |df$Country == "Mexico")
{df$Continent <- "North America"}
if(df$Country == "Spain" |df$Country == "France" |df$Country == "Germany")
{df$Continent <- "Europe"}
## .. etc
summary(df)
Run Code Online (Sandbox Code Playgroud)
但是,每当我运行df时,我发现它将北美分配给所有国家.我知道这可能听起来很微不足道,但是如果我if在任何地方使用法规而不是else或者它会有所不同if else吗?有任何纠正这个的建议吗?
Rggplot2问题。
我们可以通过 得到默认的颜色顺序RColorBrewer::brewer.pal(n, palette.name)。形状的顺序怎么样?
R 中可用的形状位于此处。我只需要订单。我知道前三个是c(16, 17, 15),即圆形、三角形和正方形。
我用R markdown编写了很长的绘图代码。我不想显示代码,但我想显示图形。
我使用了这个建议并将其放在我的R块中
```{r, include=FALSE, results="hide", fig.width=8, fig.height=6}
x = c(1:10)
y = c(40:50)
plot(x~y)
````
Run Code Online (Sandbox Code Playgroud)
但是,这根本不会在编织文件上提供任何输出。有什么建议么?
我正在尝试在 R 中绘制一个图。我的 x 轴是转换为因子的周数,而我的 y 轴是一个数量。
当我跑plot()而不是点时,我会得到水平线。
为什么会发生这种情况?
这是一个示例数据集:
df <- data.frame(fin_week=as.factor(seq(1,20, by =1)), amount=(rnorm(20)^2)*100)
plot(df)
Run Code Online (Sandbox Code Playgroud) 在 ioslides 中,您可以通过按“w”使幻灯片宽屏。css 或 yaml 标头中是否有任何设置可以使宽屏成为默认设置?
r ×10
ggplot2 ×4
plot ×2
r-markdown ×2
facet-wrap ×1
geom-bar ×1
if-statement ×1
ioslides ×1
knitr ×1
list ×1
nested ×1
shapes ×1
subset ×1