ggplot2: merge legends for geom

In a ggplot2 plot, I am combining geom_line and geom_point with geom_bar and am having problems merging the legends into a single box. Code for the basic plot is below. The data used is further down. # Packages library(ggplot2) library(scales) # Basic Plot ggplot(data = df1, aes(x = Year, y = value, group = variable, colour = variable, shape = variable)) + geom_line() + geom_point(siz

ggplot2:合并geom的图例

在ggplot2图中,我将geom_line和geom_point与geom_bar结合geom_line ,并且在将图例合并到一个盒子时遇到问题。 基本情节代码如下。 使用的数据进一步下降。 # Packages library(ggplot2) library(scales) # Basic Plot ggplot(data = df1, aes(x = Year, y = value, group = variable, colour = variable, shape = variable)) + geom_line() + geom_point(size = 3) + geom_bar(data = df2, aes(x = Year, y = valu

convert factor with 11 levels to character or numeric

I have the following data frame (imported from a csv) with a row of factors with 11 levels. > df_ave[1,] ExtCal.Average ExtCal.Average.1 ExtCal.Average.2 ExtCal.Average.3 ExtCal.Average.4 ExtCal.Average.5 Isotope 7Li 11B 31P 63Cu 66Zn 85Rb ExtCal.Average.6 ExtCal.Average.7 ExtCal.Average.8 ExtCal.Average.

将11个级别的因子转换为字符或数字

我有以下数据框(从csv导入)与一系列11个级别的因素。 > df_ave[1,] ExtCal.Average ExtCal.Average.1 ExtCal.Average.2 ExtCal.Average.3 ExtCal.Average.4 ExtCal.Average.5 Isotope 7Li 11B 31P 63Cu 66Zn 85Rb ExtCal.Average.6 ExtCal.Average.7 ExtCal.Average.8 ExtCal.Average.9 ExtCal.Average.10 ExtCal.Average.11 Is

How can I convert a factor variable with missing values to a numeric variable?

I loaded my dataset (original.csv) to R: original <- read.csv("original.csv") str(original) showed that my dataset has 16 variables (14 factors, 2 integers). 14 variables have missing values. It was OK, but 3 variables that are originally numbers, are known as factors. I searched web and get a command as: as.numeric(as.character(original$Tumor_Size)) (Tumor_Size is a variable t

如何将具有缺失值的因子变量转换为数值变量?

我将我的数据集(original.csv)加载到R:original < - read.csv(“original.csv”) str(原始)显示我的数据集有16个变量(14个因子,2个整数)。 14个变量缺少值。 这是可以的,但最初的数字是3个变量,被称为因子。 我在网上搜索并得到一个命令:as.numeric(as.character(original $ Tumor_Size))(Tumor_Size是一个被称为因子的变量)。 顺便说一下,我的数据集中的缺失值标记为点(。) 运行后:as.numer

R: converting some levels of a factor to numeric

I am new to R. I have a factor object with >10000 levels. Most of these levels are numbers which I would like to convert to numeric, but there are several levels that contain descriptions which I do not wish to touch. Is there a good way for me to isolate the relevant levels of the factor to convert into numeric? Would as.numeric() be a good choice? Many thanks! 由于我们不能有一个具有多

R:将因子的某些级别转换为数字

我是R新手。我有一个具有> 10000级别的因子对象。 大多数这些级别都是我想要转换为数字的数字,但有几个级别包含我不想触及的说明。 有没有一种很好的方法来隔离要转换为数字的因素的相关级别? as.numeric()会是一个不错的选择吗? 非常感谢! 由于我们不能有一个具有多个类的列/向量,因此一个建议是仅将numeric元素进行子集化并使用as.numeric(as.character(将其转换回numeric as.numeric(as.character( i1 <-

How to preserve original values in a variable turned into a factor?

Here's some working code to illustrate my question: # Categorical variable recorded as numeric (integer) df1 <- data.frame(group = c(1, 2, 3, 9, 3, 2, 9, 1, 9, 3, 2)) I have a categorical variable ( group ) recorded as integer values. For plots and to include this variable in models, it would be useful to have it encoded as factor, mapping each number to a label describing the category.

如何保存变量中的原始值变成一个因子?

这里有一些工作代码来说明我的问题: # Categorical variable recorded as numeric (integer) df1 <- data.frame(group = c(1, 2, 3, 9, 3, 2, 9, 1, 9, 3, 2)) 我有一个分类变量( group )记录为整数值。 对于图和将这个变量包含在模型中,将它编码为因子将会很有用,将每个数字映射到描述该类别的标签。 所以我创造了一个因素: # Make it a factor df1$group_f <- factor(x = df1$group, l

From Factor to Numeric or Integer error

I have a dataframe in R that I loaded from a CSV file. One of the variables is called "Amount" and is meant to contain positive and negative numbers. When I looked at the dataframe, this variable's datatype is listed as a factor, and I need it in a numeric format (Not sure which kind though - integer - numeric, umm...?). So, I tried to convert it to one of those two formats but

从因子到数字或整数错误

我有一个从CSV文件加载的R数据框。 其中一个变量称为“金额”,意在包含正数和负数。 当我查看数据框时,这个变量的数据类型被列为一个因子,我需要它的数字格式(不知道哪种 - 整数 - 数字,umm ...?)。 所以,我试图将它转换为这两种格式之一,但看到了一些有趣的行为。 初始数据帧: str(df) Amount : Factor w/ 11837 levels "","-1","-10",..: 2 2 1664 4 6290 6290 6290 6290 6290 6290 ... 正如我上面提

Convert Factor columns in data frame to numeric type columns

Possible Duplicates: Convert factor to integer R - How to convert a factor to an integernumeric in R without a loss of information I have read a text file where some of the columns with real number are read as factors into a data frame. How do i convert the factoe columns into numeric columns 您可以使用as.numeric(as.character(x)) The reason that your column with numbers got read in as a

将数据框中的因子列转换为数字类型列

可能重复: 将因子转换为整数 R - 如何在R中将因子转换为整数数字而不会丢失信息 我已经阅读了一个文本文件,其中一些具有实数的列被读入数据框中。 我如何将factoe列转换为数字列 您可以使用as.numeric(as.character(x)) 之所以将带数字的列作为因子读入,是因为某些地方会导致列不是数字,或者是弄乱了小数字符(这是第一个问题的特例)。 如果你的小数不是. ,你可以通过参数dec指定一个新的,例如dec = ",&

Convert factor to integer

This question already has an answer here: How to convert a factor to integernumeric without loss of information? 5 answers You can combine the two functions; coerce to characters thence to numerics: > fac <- factor(c("1","2","1","2")) > as.numeric(as.character(fac)) [1] 1 2 1 2 Quoting directly from the help page for factor : To transform a factor f to its original numeric valu

将因子转换为整数

这个问题在这里已经有了答案: 如何将因子转换为整数数字而不丢失信息? 5个答案 你可以结合这两个功能; 从而将角色强制为数字: > fac <- factor(c("1","2","1","2")) > as.numeric(as.character(fac)) [1] 1 2 1 2 直接从帮助页面引用factor : 要将因子f转换为其原始数值,建议使用as.numeric(levels(f))[f] ,并且要比as.numeric(as.character(f))稍微高效。

Reorder levels of a factor without changing order of values

I have data frame with some numerical variables and some categorical factor variables. The order of levels for those factors is not the way I want them to be. numbers <- 1:4 letters <- factor(c("a", "b", "c", "d")) df <- data.frame(numbers, letters) df # numbers letters # 1 1 a # 2 2 b # 3 3 c # 4 4 d If I change the order of the level

在不改变数值顺序的情况下重新排列因子的水平

我有一些数值变量和一些分类factor变量的数据框。 这些因素的级别顺序并不是我希望它们成为的方式。 numbers <- 1:4 letters <- factor(c("a", "b", "c", "d")) df <- data.frame(numbers, letters) df # numbers letters # 1 1 a # 2 2 b # 3 3 c # 4 4 d 如果我改变关卡的顺序,那么这些关键字就不再有相应的数字了(我的数据从这个意义上说是无稽之谈)。 lev

Arithmetic operations on R factors

I have an R dataframe and I'm trying to subtract one column from another. I extract the columns using the $ operator but the class of the columns is 'factor' and R won't perform arithmetic operations on factors. Are there special functions to do this? If you really want the levels of the factor to be used, you're either doing something very wrong or too clever for its own

R因子的算术运算

我有一个R数据框,我试图从另一列中减去一列。 我使用$运算符提取列,但列的类是'因子',R不会对因子执行算术运算。 是否有特殊的功能来做到这一点? 如果你真的想要使用这个因子的水平,那么你要么为了自己的利益而做一些非常错误的事情或者太巧妙。 如果你所拥有的是一个包含存储在因子水平中的数字的因子,那么你首先要使用as.numeric(as.character(...))将它强制转换为数字: dat <- data.frame(f=as.chara