2

I would like to know if there is a way to replace the label in the x-axis with the value from a different column. Suppose I have this data frame.

Chromosome  Value  Region
chr1        2      centromere
chr1        2.1    telomere
chr2        2.2    centromere
chr2        1.9    telomere
chr3        2      centromere
chr3        2      telomere

I would like to plot "Region" (x-axis) against "Value" (y-axis), adjust the data point shape according to "Region", but change the x-axis label according to "Chromosome". I tried to plot "Chromosome" against "Value" but the data points become stacked as shown below.

enter image description here

Plotting "Region" against "Value" would create a correct plot but the x-axis label would not be very informative. I want the data points to be to not stack like the figure above. Any tips or help is really appreciated.

1
  • Could you please include a minimal dataset in order to test and verify possible solutions? see minimal reproducible example, you could try adding your data with dput("your_dataframe") if the data frame is not too large. Best to generate a subset with the absolute minimal data which illustrates your question. Commented Jun 21, 2020 at 10:36

1 Answer 1

2

This can be achieved by adding position = position_dodge(width = 0.9) to geom_point:

d <- read.table(text = "Chromosome  Value  Region
chr1        2      centromere
chr1        2.1    telomere
chr2        2.2    centromere
chr2        1.9    telomere
chr3        2      centromere
chr3        2      telomere", header = TRUE)

library(ggplot2)

ggplot(d, aes(Chromosome, Value, shape = Region, color = Chromosome)) +
  geom_point(position = position_dodge(width = 0.9))

Created on 2020-06-21 by the reprex package (v0.3.0)

Sign up to request clarification or add additional context in comments.

1 Comment

width = 0.9 is a bit random;) , and I'd probably add group = Region just to make sure it does dodge on the correct group... +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.