0

I am new to R and I (probably) have a simple question, but I am unable to get my head around it. Even though I have been trying it in multiple ways:

I want to make a plot from a subgroup of a certain classification (aneurysm type) as follows:

ggplot(df, aes(x= MSS, y= Aneurysm_Volume)) +
  geom_point(aes(x = df$TF1_months, y = df$AV1[df$xaneurysm_type == 1])) +
  geom_point(aes(x = df$TF2_months, y = df$AV2[df$xaneurysm_type == 1])) + 
  geom_point(aes(x = df$TF3_months, y = df$AV3[df$xaneurysm_type == 1])) +
  geom_point(aes(x = df$TF4_months, y = df$AV4[df$xaneurysm_type == 1])) +
  geom_point(aes(x = df$TF5_months, y = df$AV5[df$xaneurysm_type == 1])) +
  xlim(0, NA) + ylim(0, NA)

However, the following error applies. Error: Aesthetics must be either length 1 or the same as the data (154): y

I tried to make a seperate variably for Y as follows:

if(df$xaneurysm_type == 1) {
   (df$juxtavolume1 = df$AV1)
} else {
  (df$juxtavolume1 = NA)
}

However, the following warning applies:

Warning message: In if (df$xaneurysm_type == 1) { : the condition has length > 1 and only the first element will be used

I have also tried to make the conditional variable the following way:

df$juxtavolume1 <- as.integer(NA)
df[which(df$xaneurysm_type == 1),]$juxtavolume1 <- as.integer(df$AV1)

But then the following error applies: Error in $<-.data.frame(*tmp*, juxtavolume1, value = c(NA_integer_, : replacement has 154 rows, data has 97

Does anybody understand where I am going wrong? Your help is much appreciated!

Warm regards

pogibas
  • 27,303
  • 19
  • 84
  • 117
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. You are probably going to want to reshape your data rather than adding a bunch of layers. – MrFlick Dec 02 '19 at 18:38
  • Part of the problem is that you're using multiple different x variables in your plot. Try plotting only a few at a time, or convert your data to long data, i.e. tidy data, instead of the wide format that you have. – Hansel Palencia Dec 02 '19 at 21:10
  • Reshape first. Never use `$` inside `aes`. You want to use `ifelse()` instead of `if () else`. – Axeman Dec 02 '19 at 22:51

0 Answers0