使用 version.string R 版本 2.11.0 (2010-04-22) quantmod "0.3-17"
Windows XP
使用该chartSeries功能时,图表上显示的线条颜色为绿色quantmod。type="line"我想将颜色从绿色更改为另一种颜色。
看起来我可以更改chartTheme,但主题没有显式地具有变量来更改线条的绘图显示颜色。
我可以在使用该功能时更改线条显示颜色 plot()- 那么是否可以使用chartSeries()in将线条图的显示更改为不同的颜色quantmod?
在ggplot生成的图中,沿x轴的每个标签都是一个字符串,即"1990年的产品".但是,生成的绘图在每个单词之间存在一段时间.换句话说,上面的字符串显示为"the.product.in.1990"
如何确保不添加上述"."?
以下代码是我用于沿x轴为每个点添加字符串的代码
last_plot()+scale_x_discrete(limits=ddata$labels$text)
Run Code Online (Sandbox Code Playgroud)
示例代码:
library(ggdendro)
x <- read.csv("test.csv",header=TRUE)
d <- as.dist(x,diag=FALSE,upper=FALSE)
hc <- hclust(d,"ave")
dhc <- as.dendrogram(hc)
ddata <- dendro_data(dhc,type="rectangle")
ggplot(segment(ddata)) + geom_segment(aes(x=x0,y=y0,xend=x1,yend=y1))
last_plot() + scale_x_discrete(limits=ddata$labels$text)
Run Code Online (Sandbox Code Playgroud)
每一行ddata$labels$text都是一个字符串,就像"1990年的产品".我想在生成的图中保留相同的格式而不是"the.product.in.1990"
我正在使用以下脚本分析大量数据:
M <- c_alignment
c_check <- function(x){
if (x == c_1) {
1
}else{
0
}
}
both_c_check <- function(x){
if (x[res_1] == c_1 && x[res_2] == c_1) {
1
}else{
0
}
}
variance_function <- function(x,y){
sqrt(x*(1-x))*sqrt(y*(1-y))
}
frames_total <- nrow(M)
cols <- ncol(M)
c_vector <- apply(M, 2, max)
freq_vector <- matrix(nrow = sum(c_vector))
co_freq_matrix <- matrix(nrow = sum(c_vector), ncol = sum(c_vector))
insertion <- 0
res_1_insertion <- 0
for (res_1 in 1:cols){
for (c_1 in 1:conf_vector[res_1]){
res_1_insertion <- res_1_insertion …Run Code Online (Sandbox Code Playgroud) 我对R很新,所以如果我的问题不清楚,请耐心等待.
我有一个data.frame有5列的"蛋白质",即;
1.protein_name,2.protein_FC,3.protein_pval,4.mRNA_FC,5.mRNA_pval和6.freq.
我试图绘制一个火山图,其中x = log2(protein_FC),y = -log10(protein_pval).然后将点的大小映射到频率和颜色到mRNA_FC.这一切都很好,这是我用过的代码:
ggplot( protein [ which ( protein$freq <= 0.05 ),] , aes( x = log2( protein_FC ) ,
y = -log10 ( protein_pval ) , size = freq , colour = mRNA_FC ,
label = paste(protein_name,",",mRNA_pval), alpha=1/1000)) +
geom_point() + geom_text( hjust = 0 , vjust = 0 , colour = "black" , size = 2.5 ) +
geom_abline( intercept = 1.3 , slope = 0) +
scale_colour_gradient(limits=c(-3,3))
Run Code Online (Sandbox Code Playgroud)
一切都很好,直到这里.但由于实验的性质,数据非常密集mRNA_FC = …
我有以下ggplot2代码,我想使用基本图形而不是ggplot2来生成类似的输出 - 但我似乎无法找到一种方法来区分多个"属性"与正常情节.我错过了什么:
GGPLOT2:
ggplot(data.df, aes(x=Axis1, y=Axis2, shape=Plant, color=Type)) +
geom_point()
Run Code Online (Sandbox Code Playgroud)
我的情节尝试(内联帮助让我有点相似):
data.ma <- as.matrix(data.df)
plot(range(data.ma[,6]), range(data.ma[,7]),xlab="Axis 1",ylab="Axis 2")
points(data.ma[data.ma[,1] == 'Plant1',6],
data.ma[data.ma[,1] == 'Plant1',7], pch=2)
points(data.ma[data.ma[,1] == 'Plant2',6],
data.ma[data.ma[,1] == 'Plant2',7], pch=3)
legend(0,legend=c("Plant1","Plant2"))
Run Code Online (Sandbox Code Playgroud)
这给了我一个图,其中至少可以在图中区分"植物"类型,但它确实看起来很复杂,我无法弄清楚如何根据"类型"行更改所有点的颜色.
有什么建议?
编辑 - 一个数据示例//我意识到我第一次尝试使用情节甚至没有给出正确的例子:(:
library(ggplot2)
data.df <- data.frame(
Plant=c('Plant1','Plant1','Plant1','Plant2','Plant2','Plant2'),
Type=c(1,2,3,1,2,3),
Axis1=c(0.2,-0.4,0.8,-0.2,-0.7,0.1),
Axis2=c(0.5,0.3,-0.1,-0.3,-0.1,-0.8)
)
ggplot(data.df, aes(x=Axis1, y=Axis2, shape=Plant, color=Type)) +
geom_point()
data.ma <- as.matrix(data.df)
plot(range(data.ma[,3]), range(data.ma[,4]),xlab="Axis 1",ylab="Axis 2")
points(data.ma[data.ma[,1] == 'Plant1',3],
data.ma[data.ma[,1] == 'Plant1',4], pch=2)
points(data.ma[data.ma[,1] == 'Plant2',3],
data.ma[data.ma[,1] == 'Plant2',4], pch=3)
legend(0,legend=c("Plant1","Plant2"))
Run Code Online (Sandbox Code Playgroud) 为了在匹配过程之前和之后直观地显示协变量平衡,我编写了以下代码:
library(lattice)
library(gridExtra)
StandBias.df= data.frame(
Category= rep(c("A", "B", "C", "D", "E", "F"), 2),
Groups = factor(c(rep("0", 6), rep("1", 6))),
Values = c(0.26, 0.4, 0.3, 0.21, 0.6, 0.14, 0.12, -0.04, -0.23, 0.08, 0.14, -0.27))
d1<-dotplot(Category ~Values, data = StandBias.df, groups = Groups,
main = "Standardized Mean Differences", col = c("black", "grey50"), pch=c(22,15), xlab=NULL,
key=list(text=list(c("Pre-Matching", "Post-Matching")),
points=list(col = c("black", "grey50"), pch=c(22,15)),
space="bottom", border=T))
Ttest.df = data.frame(
Category= rep(c("A", "B", "C", "D", "E", "F"), 2),
Groups = factor(c(rep("0", 6), rep("1", 6))),
Values = …Run Code Online (Sandbox Code Playgroud) 因此,我在R中遇到了这个奇怪的错误。我有一个简单的函数,当比较实际价格和模拟价格时会返回错误项hestondifferences()。
当我尝试通过以下方式找到本地最小值时:
res<-optim(fn=hestondifferences, par = c(vT=vT, rho=rho, k=k, sigma=sigma))
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:
optim中的误差(fn = hestondifferences,par = c(vT = vT,rho = rho,k = k,:无法在初始参数处评估函数
令我感到困惑的是,直接使用初始参数调用该函数会hestondifferences(vT, rho, k, sigma)返回正确的值。
该函数hestondifferences()以某种方式编写:只要无法对某些参数集进行仿真,它将返回NA与optim()期望相符的结果。
为了读取data.table切片,我可以使用以下语法:
foo = DT[, 5:10, with=F]
Run Code Online (Sandbox Code Playgroud)
但现在我想做:
foo = foo + 1
DT[, 5:10, with=F] = foo
Run Code Online (Sandbox Code Playgroud)
这不起作用; 按名称引用列似乎也不起作用.有什么建议?
好吧,我正试着用dplyr包裹我,而不是使用plyr.在我与R的短暂时间里,我已经习惯了ddply.我正在使用一个"简单"的例子来说明如何使用dplyr而不是plyr中的ddply.这里是:在以下内容中:
t1.table <- ddply(diamonds, c("clarity", "cut"), "nrow")
Run Code Online (Sandbox Code Playgroud)
我收到了一份清晰和切割的钻石数量汇总表.在dplyr中,我能想出的最简单的例子是:
diamonds %>% select(clarity, cut) %>% group_by(clarity, cut) %>%
summarise(count=n()) -> t2.table
Run Code Online (Sandbox Code Playgroud)
这似乎有点涉及.有没有更好的方法来简化这个?〜谢谢
这是我可重复的数据:
library("ggplot2")
library("ggplot2movies")
library("quantreg")
set.seed(2154)
msamp <- movies[sample(nrow(movies), 1000), ]
Run Code Online (Sandbox Code Playgroud)
我正在尝试熟悉stat_quantile,但文档中的示例提出了几个问题.
mggp <- ggplot(data=msamp, mapping=aes(x=year, y=rating)) +
geom_point() +
stat_quantile(formula=y~x, quantiles=c(0, 0.25, 0.50, 0.75, 1)) +
theme_classic(base_size = 12) +
ylim(c(0,10))
mggp
Run Code Online (Sandbox Code Playgroud)
根据我的理解,分位数将数据分成小于定义的截止值的部分,对吗?如果我像下面的代码中那样定义分位数,我得到五行.为什么?它们代表什么?
似乎分位数是基于y轴上的因变量(评级)计算的.有可能扭转这种局面吗?我的意思是根据"年份"中的分位数分割数据?