R 使用ggplot2(或其他)绘制两个变量
我有一个数据集,名为fruits\u by\u number,如下所示:R 使用ggplot2(或其他)绘制两个变量,r,ggplot2,visualization,R,Ggplot2,Visualization,我有一个数据集,名为fruits\u by\u number,如下所示: category Number Apple 4 Banana 5 Orange 2 Pear 0 p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = category)) p + geom_bar(stat = "identity") p <- ggplot(data = fr
category Number
Apple 4
Banana 5
Orange 2
Pear 0
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = category))
p + geom_bar(stat = "identity")
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = Age))
p + geom_bar(stat = "identity", position = "dodge")
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = category))
p <- p + geom_bar(stat = "identity")
p <- p + theme(axis.text.x = element_blank())
p
我想做一个图形,在x轴上有条表示水果类别,在y轴上有计数作为高度
我试过这个:
qplot(x = category , y = number, data = fruits_by_number, geom = "dot")
我的第一个问题是:我如何代替dot要求一个酒吧
我的第二个问题是:即使我得到了一个条,因为在实际的数据集中,水果的名字大约是30个,在x轴的图例下面,我得到了一个很大的混乱
我看到了:
qplot(data = movies_df,Runtime,fill = Genre,bins = 30)
上面有一张图片:
这和我想要的很接近
我试着用道奇来代替填充,但没用
你能帮我做一个图表,让水果类别按颜色排列在右边,就像图中一样,然后我会有一个高度等于数字的colorfur非重叠条吗
PS1:如果我遵循这个建议:
p <- ggplot(data = trans_units_by_subject, aes(x = category, y = sumUnits_t, fill = category))
p + geom_bar(stat = "identity")
我仍然可以在x轴下面看到这样的描述:这里有一个使用内置数据集的可能解决方案:
mtcars %>%
mutate(CARS = factor(1:32)) %>%
ggplot(aes(CARS,mpg, fill = CARS)) +
geom_bar(stat = "identity")
以下是使用内置数据集的可能解决方案:
mtcars %>%
mutate(CARS = factor(1:32)) %>%
ggplot(aes(CARS,mpg, fill = CARS)) +
geom_bar(stat = "identity")
我建议把你的情节转向侧面,以适应30个类别。使用上述数据
require(ggplot2)
ggplot(fruits_by_number) +
geom_bar(aes(x = category,
y = Number,
fill = category),
stat = "identity") +
coord_flip()
如果上面的填充将颜色添加到条形图中,请删除此选项以使其颜色相同,并且数据为
fruits_by_number <-
read.table(text='
category Number
Apple 4
Banana 5
Orange 2
Pear 0',
header=T)
我建议把你的情节转向侧面,以适应30个类别。使用上述数据
require(ggplot2)
ggplot(fruits_by_number) +
geom_bar(aes(x = category,
y = Number,
fill = category),
stat = "identity") +
coord_flip()
如果上面的填充将颜色添加到条形图中,请删除此选项以使其颜色相同,并且数据为
fruits_by_number <-
read.table(text='
category Number
Apple 4
Banana 5
Orange 2
Pear 0',
header=T)
我建议您使用ggplot而不是qplot。我相信这是更容易管理的,你可以发挥更好的选择的情节。试着这样做:
category Number
Apple 4
Banana 5
Orange 2
Pear 0
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = category))
p + geom_bar(stat = "identity")
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = Age))
p + geom_bar(stat = "identity", position = "dodge")
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = category))
p <- p + geom_bar(stat = "identity")
p <- p + theme(axis.text.x = element_blank())
p
这将生成一个条形图,其中X轴将是水果类别,Y轴将是数字,并且您将为该图中的每个水果(包括其图例)进行颜色填充。我必须告诉你们,在这种图形中使用颜色填充是多余的,不包括任何美学功能,因为所有的水果在X轴上都有标签
另一件事,当你有两个因素和几个级别时,你可以使用道奇。例如,假设您的数据框中有一个额外的因素年龄:Apple New/Old、Pear New/Old。所以你可以用这样的方法:
category Number
Apple 4
Banana 5
Orange 2
Pear 0
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = category))
p + geom_bar(stat = "identity")
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = Age))
p + geom_bar(stat = "identity", position = "dodge")
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = category))
p <- p + geom_bar(stat = "identity")
p <- p + theme(axis.text.x = element_blank())
p
生成的绘图将只有两种颜色用于factor AgeNew/Old,条形图将分开。在您放置的示例中,默认选项是position=stack,因此您将拥有堆叠的条
如果仍要使用颜色而不使用x轴标签,请使用类似以下内容:
category Number
Apple 4
Banana 5
Orange 2
Pear 0
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = category))
p + geom_bar(stat = "identity")
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = Age))
p + geom_bar(stat = "identity", position = "dodge")
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = category))
p <- p + geom_bar(stat = "identity")
p <- p + theme(axis.text.x = element_blank())
p
并尝试不同的调色板
请查看以下页面:
我建议您使用ggplot而不是qplot。我相信这是更容易管理的,你可以发挥更好的选择的情节。试着这样做:
category Number
Apple 4
Banana 5
Orange 2
Pear 0
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = category))
p + geom_bar(stat = "identity")
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = Age))
p + geom_bar(stat = "identity", position = "dodge")
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = category))
p <- p + geom_bar(stat = "identity")
p <- p + theme(axis.text.x = element_blank())
p
这将生成一个条形图,其中X轴将是水果类别,Y轴将是数字,并且您将为该图中的每个水果(包括其图例)进行颜色填充。我必须告诉你们,在这种图形中使用颜色填充是多余的,不包括任何美学功能,因为所有的水果在X轴上都有标签
另一件事,当你有两个因素和几个级别时,你可以使用道奇。例如,假设您的数据框中有一个额外的因素年龄:Apple New/Old、Pear New/Old。所以你可以用这样的方法:
category Number
Apple 4
Banana 5
Orange 2
Pear 0
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = category))
p + geom_bar(stat = "identity")
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = Age))
p + geom_bar(stat = "identity", position = "dodge")
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = category))
p <- p + geom_bar(stat = "identity")
p <- p + theme(axis.text.x = element_blank())
p
生成的绘图将只有两种颜色用于factor AgeNew/Old,条形图将分开。在您放置的示例中,默认选项是position=stack,因此您将拥有堆叠的条
如果仍要使用颜色而不使用x轴标签,请使用类似以下内容:
category Number
Apple 4
Banana 5
Orange 2
Pear 0
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = category))
p + geom_bar(stat = "identity")
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = Age))
p + geom_bar(stat = "identity", position = "dodge")
p <- ggplot(data = fruits_by_number, aes(x = category, y = Number, fill = category))
p <- p + geom_bar(stat = "identity")
p <- p + theme(axis.text.x = element_blank())
p
并尝试不同的调色板
请查看以下页面:
如果你有30种水果,颜色无法区分它们。任何超过8种颜色的东西都很难区分。如果你有30种水果,颜色无法区分它们。任何超过8种颜色的东西都很难区分。将填充映射到mpg而不是CARS可能是更好的做法,因为按生成的顺序ID着色可提供0个附加信息。将填充映射到mpg而不是CARS可能是更好的做法,因为按生成的顺序ID着色可提供0个附加信息信息。谢谢你-你说所有的水果都会在X轴上有标签。你是对的,但我正在努力避免。我的分类大约有30个,所以在x轴上我看不到清晰的东西。那么,在使用legentd描述颜色填充时,是否可以不在底部显示类别图例?我已经用x下面的传奇图片更新了我的问题。第二个建议效果很好我刚刚在PS1中包含了这张图片,只是想告诉你为什么我不想给x轴贴标签。很高兴这对我有帮助!我希望您继续使用ggplot2。为供日后参考,请参阅
呃,检查我在答案末尾的链接。你可以使用主题来改变很多东西,包括轴标签的外观、字体、字体大小等。试着用这个代替元素空白,然后你可以在z度内旋转轴标签:themeaxis.text.x=element\u textangle=zThank You-你说所有的水果都会在x轴上有标签你是对的,但我正试图避免这种情况。我的分类大约有30个,所以在x轴上我看不到清晰的东西。那么,在使用legentd描述颜色填充时,是否可以不在底部显示类别图例?我已经用x下面的传奇图片更新了我的问题。第二个建议效果很好我刚刚在PS1中包含了这张图片,只是想告诉你为什么我不想给x轴贴标签。很高兴这对我有帮助!我希望您继续使用ggplot2。为了将来的参考,记得检查我在答案末尾的链接。您可以使用该主题更改许多内容,包括轴标签的外观、字体、字体大小等。请尝试使用该主题而不是“元素”空白,您可以在z度内旋转轴标签:themeaxis.text.x=element\u textangle=z非常有帮助谢谢-旋转确实有帮助。我甚至不知道这是一个选择。当然。使用ggplot2,您几乎可以绘制任何您想要的内容。这是一些关于基础知识的在线文档:非常有用,谢谢-轮换真的很有帮助。我甚至不知道这是一个选择。当然。使用ggplot2,您几乎可以绘制任何您想要的内容。以下是有关基础知识的在线文档: