Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

r - ggplot not seperating by value - Calling variables that start with numbers

I am trying to create a violin plot with genotype on the x axis and age on the y axis.

My data.frame (df1) looks like this:

ID 1kbDel 3kbDel Age
1  WT     Homo      40
2  Het    WT        30
3  Homo   Het       60

I tried:

ggplot(df1, aes(x='1kbDel', y=Age)) + geom_violin(trim=FALSE)

Where I also changed the values of age, 1kbDel, and 3kbDel to factors. But instead of creating a plot with 3 violins (WT, Het, Homo), it creates one labeled '1kb'

I also tried changed Wt, Het, Homo to 0, 1, 2 but I get the same behavior, I dont know why

question from:https://stackoverflow.com/questions/65545971/ggplot-not-seperating-by-value-calling-variables-that-start-with-numbers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use backticks to refer to variable name that start with number.

library(ggplot2)
ggplot(df1, aes(x=`16kb`, y=Age)) + geom_violin()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...