我试图使用ggplot2包绘制下面的图形,但不知何故轴不会显示.蜱虫在那里,而不是轴线.我已经使用过该theme(axis.line=element_line())功能,但它不起作用.
这是我的代码:
library(ggplot2)
ggplot(data = soepl_randsub, aes(x = year, y =satisf_org, group = id)) +
geom_point() + geom_line() +ylab("Current Life Satisfaction") +theme_bw() +
theme(plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank() ) +
theme(panel.border= element_blank()) +
theme(axis.line = element_line(color="black", size = "2"))
Run Code Online (Sandbox Code Playgroud)
我不确定出了什么问题.这是图表.
如何在列中按组聚合数据group并在列中折叠文本text?
样本数据:
df <- read.table(header=T, text="
group text
a a1
a a2
a a3
b b1
b b2
c c1
c c2
c c3
")
Run Code Online (Sandbox Code Playgroud)
所需输出(数据框):
group text
a a1a2a3
b b1b2
c c1c2c3
Run Code Online (Sandbox Code Playgroud)
我现在有:
sapply(unique(df$group), function(x) {
paste0(df[df$group==x,"text"], collapse='')
})
Run Code Online (Sandbox Code Playgroud)
这在某种程度上起作用,因为它返回按组正确折叠的文本,但作为向量:
[1] "a1a2a3" "b1b2" "c1c2c3"
Run Code Online (Sandbox Code Playgroud)
我需要一个带有group列的数据框.
考虑(如果rstudio)将在浏览器中打开某些内容的功能,如果y = TRUE在浏览器中和浏览器中y = FALSE.您可以强制whatever在浏览器中打开options(viewer = NULL)(然后需要重置为之前),但我无法使用常规on.exit方法在函数内部使用它.在Windows和osx上测试.
f <- function(x, y = TRUE) {
if (y) {
oo <- getOption('viewer')
on.exit(options(viewer = oo))
options(viewer = NULL)
} else options(viewer = NULL)
print(getOption('viewer'))
DT::datatable(x)
}
g <- function(x, y = TRUE) {
if (y) {
oo <- getOption('viewer')
on.exit(options(viewer = oo))
options(viewer = NULL)
} else options(viewer = NULL)
print(getOption('viewer'))
htmlTable::htmlTable(x)
}
## in rstudio, returns the viewer function
getOption('viewer')
# …Run Code Online (Sandbox Code Playgroud) 我想在hline情节中添加一个图例.
我子集的头部看起来像这样
Site Date Al
1 Bo6 2014-10-07 152.1
2 Bo1 2014-10-07 157.3
3 Bo3 2014-10-07 207.1
4 Bo4 2014-10-07 184.3
5 Bo5 2014-10-07 23.2
13 Bo6 2014-10-14 96.8
Run Code Online (Sandbox Code Playgroud)
我的代码如下:
require(ggplot2)
require(reshape2)
require(magrittr)
require(dplyr)
require(tidyr)
setwd("~/Documents/Results")
mydata <- read.csv("Metals sheet Rwosnb5.csv")
mydata <- read.csv("Metals sheet Rwosnb5.csv")
L <- subset(mydata, Site =="Bo1"| Site == "Bo2"| Site == "Bo3"| Site == "Bo4"| Site == "Bo5" | Site == "Bo6", select = c(Site,Date,Al))
L$Date <- as.Date(L$Date, "%d/%m/%Y")
I <- ggplot(data=L, aes(x=Date, y=Al, colour=Site)) …Run Code Online (Sandbox Code Playgroud) 我正在使用ggplot制作箱图,其数据按2个因子变量分类.我想让盒子尺寸反映样品尺寸,varwidth = TRUE但是当我这样做时盒子会重叠.
1)具有3×2结构的一些样本数据
data <- data.frame(group1= sample(c("A","B","C"),100, replace = TRUE),group2= sample(c("D","E"),100, replace = TRUE) ,response = rnorm(100, mean = 0, sd = 1))
Run Code Online (Sandbox Code Playgroud)
2)默认的箱形图:没有可变宽度的ggplot
ggplot(data = data, aes(y = response, x = group1, color = group2)) + geom_boxplot()
Run Code Online (Sandbox Code Playgroud)

我喜欢如何显示第一级分组.
现在我尝试添加可变宽度......
3)......以及我什么时候得到的 varwidth = TRUE
ggplot(data = data, aes(y = response, x = group1, color = group2)) + geom_boxplot(varwidth = T)
Run Code Online (Sandbox Code Playgroud)

无论是在主要调用中还是在语句中使用color = group2或group = group2同时出现这种重叠.大惊小怪似乎也没有帮助.ggplotgeom_boxplotposition_dodge
4)我不喜欢的解决方案是通过组合我的group1和group2来制作独特的因素
data$grp.comb <- paste(data$group1, data$group2) …Run Code Online (Sandbox Code Playgroud) 我可以轻松地在CRAN上的包描述中遵循Circlize示例:
library('circlize')
set.seed(123)
mat = matrix(sample(1:100, 18, replace = TRUE), 3, 6)
rownames(mat) = letters[1:3]
colnames(mat) = LETTERS[1:6]
### basic settings
par(mfrow = c(3, 2))
par(mar = c(1, 1, 1, 1))
chordDiagram(mat)
Run Code Online (Sandbox Code Playgroud)
但是,当我替换mat为myMatrixI时出现此错误:
Error in circos.initialize(factors = factor(cate, levels = cate), xlim = cbind(rep(0, :
Since `xlim` is a matrix, it should have same number of rows as the length of the level of `factors` and number of columns of 2.
Run Code Online (Sandbox Code Playgroud)
有人可以解释我收到这条消息的原因吗?我看不出差别之间mat,并myMatrix比其他 …
我正在尝试使用以下代码在此处(sthda.com)复制示例:
# Change point shapes and colors manually
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
scale_shape_manual(values=c(3, 16, 17))+
scale_color_manual(values=c('#999999','#E69F00', '#56B4E9'))+
theme(legend.position="top")
Run Code Online (Sandbox Code Playgroud)
该网页上的示例说该代码应产生以下结果:

但是,当我在R中运行它时,出现以下错误:
“错误:连续值提供给离散刻度”
有人知道这段代码可能出什么问题吗?还是为什么我得到的结果与示例不同?
如果有人可以运行示例代码并告诉我他们是否遇到相同的错误,我将不胜感激。
我想知道为什么要ifelse(1<2,print("true"),print("false"))回来
[1] "true"
[1] "true"
Run Code Online (Sandbox Code Playgroud)
而ifelse(1<2,"true","false")回报
[1] "true"
Run Code Online (Sandbox Code Playgroud)
我不明白为什么print内部ifelse返回"true"两次
我有一个包含电影数据的表格,在最后一列中,它有电影所属的类别.
movieId title category
1 Toy Story (1995) Animation|Children|Comedy
2 Jumanji (1995) Adventure|Children|Fantasy
3 Grumpier Old Men (1995) Comedy|Romance
4 Waiting to Exhale (1995) Comedy|Drama
5 Father of the Bride Part II (1995) Comedy
6 Heat (1995) Action|Crime|Thriller
Run Code Online (Sandbox Code Playgroud)
我想为每个类别创建一个列,如果它写在该电影的列表中则放1,否则放0.就像是:
movieId title animation comedy drama
1 xx 1 0 1
2 xy 1 0 0
3 yy 1 1 0
Run Code Online (Sandbox Code Playgroud)
到目前为止,我只将字符串转换为列表:
f<-function(x) {strsplit(x, split='|', fixed=TRUE)}
movies2$m<-lapply(movies2$category, f)
Run Code Online (Sandbox Code Playgroud)
但我不知道如何做其余的事情.
我在考虑使用Python词典.但我不知道如何在R中这样做.
数据
df1 <- read.table(header = TRUE, stringsAsFactors = FALSE,
text = " …Run Code Online (Sandbox Code Playgroud) 使用geom_blank我想添加一些新的因子水平,但我似乎无法做到这一点并保持NA水平
library('ggplot2')
pl <- ggplot(data.frame(x = factor(c(1:2, NA)), y = 1), aes(x, y)) + geom_point()
pl
Run Code Online (Sandbox Code Playgroud)
pl + geom_blank(data = data.frame(x = addNA(factor(c(0:3, NA))), y = 1))
Run Code Online (Sandbox Code Playgroud)
我希望x在0,1,2,3,NA使用 geom_blank
r ×10
ggplot2 ×5
axis ×1
circlize ×1
colors ×1
dataframe ×1
geom-hline ×1
if-statement ×1
legend ×1
plot ×1
rstudio ×1
scatter ×1
scatter-plot ×1
split ×1