logo
Tags down

shadow

Scatter plot R for multiple values


By : tobi
Date : September 16 2020, 07:00 PM
like below fixes the issue You can use tidyr package and the function pivot_longer to reshape your data for ggplot2:
code :
library(tidyr)
library(dplyr)
df %>% pivot_longer(., - Strain, names_to = "Variable", values_to = "Value")

# A tibble: 18 x 3
   Strain Variable       Value
    <int> <chr>          <dbl>
 1      1 second_hour   19354.
 2      1 second_hour_n 12103.
 3      1 Beula         21674.
 4      2 second_hour   20021.
 5      2 second_hour_n 11578.
 6      2 Beula         21308.
 7      3 second_hour   16106.
 8      3 second_hour_n 14426.
 9      3 Beula         19905.
10      4 second_hour   18993.
11      4 second_hour_n 12149.
12      4 Beula         20817.
13      5 second_hour   15541.
14      5 second_hour_n 15371.
15      5 Beula         20017.
16      6 second_hour   14767.
17      6 second_hour_n 16288.
18      6 Beula         19000.
library(tidyr)
library(dplyr)
library(ggplot2)
df %>% pivot_longer(., - Strain, names_to = "Variable", values_to = "Value") %>%
  ggplot(aes(x = Strain, y = Value, color = Variable))+
  geom_point()
library(tidyr)
library(dplyr)
library(ggplot2)
df$Strain <- as.factor(df$Strain)
df %>% pivot_longer(., - Strain, names_to = "Variable", values_to = "Value") %>%
  ggplot(aes(x = Strain, y = Value, color = Variable))+
  geom_point()
hour_2$Strain <- as.numeric(as.vector(hour_2$Strain))
hour_2$second_hour <- as.numeric(as.vector(hour_2$second_hour))
hour_2$second_hour_n <- as.numeric(as.vector(hour_2$second_hour_n))
structure(list(Strain = 1:6, second_hour = c(19354.4444444444, 
20021.2222222222, 16105.9444444444, 18993.3888888889, 15541.3888888889, 
14767.1666666667), second_hour_n = c(12103.3628274451, 11577.7991047524, 
14425.8808435683, 12149.3204615723, 15370.8433645383, 16288.3635541566
), Beula = c(21673.72, 21307.61, 19905.39, 20816.78, 20016.94, 
19000.44)), class = "data.frame", row.names = c(NA, -6L))
structure(list(Strain = c(1L, 2L, 21L, 44L, 5L, 6L), second_hour = c(19354.4444444444, 
20021.2222222222, 16105.9444444444, 18993.3888888889, 15541.3888888889, 
14767.1666666667), second_hour_n = c(12103.3628274451, 11577.7991047524, 
14425.8808435683, 12149.3204615723, 15370.8433645383, 16288.3635541566
), Beula = c(21673.72, 21307.61, 19905.39, 20816.78, 20016.94, 
19000.44)), class = "data.frame", row.names = c(NA, -6L))


Share : facebook icon twitter icon

Python Scatter Plot with Multiple Y values for each X


By : admechanics
Date : March 29 2020, 07:55 AM
To fix this issue I am trying to use Python to create a scatter plot that contains two X categories "cat1" "cat2" and each category has multiple Y values. I can get this to work if the number of Y values for each X value is the same by using this following code: ,
How can I plot different numbers of Y values for each X value
code :
for xe, ye in zip(x, y):
    plt.scatter([xe] * len(ye), ye)
plt.xticks([1, 2])
plt.axes().set_xticklabels(['cat1', 'cat2'])
import matplotlib.pyplot as plt
import numpy as np

y = [(1,1,2,3,9),(1,1,2,4)]
x = [1,2]

for xe, ye in zip(x, y):
    plt.scatter([xe] * len(ye), ye)

plt.xticks([1, 2])
plt.axes().set_xticklabels(['cat1', 'cat2'])

plt.savefig('t.png')

Scatter plot with multiple Y values with line for each category X label


By : william
Date : March 29 2020, 07:55 AM
wish helps you You can achieve this by shifting the highCont along the x axis by a certain amount, then using plt.plot() to draw lines between them.
I have used the variable shift to move the highCont values along the x axis by 0.2 in the example below.
code :
import matplotlib.pyplot as plt
import numpy as np

IDs = ['a', 'b', 'c', 'd', 'e', 'f']

lowCont = [-0.31, 0.71, 0.37, 0.05, 0.15, 1.33]
highCont = [-0.38, -0.16, 0.02, -0.55, -0.02, -0.51]

lowContErr = [0.03,0.13,0.02,0.10,0.09,0.04]
highContErr = [0.07, 0.09, 0.03, 0.09, 0.06, 0.03]

shift = 0.2   # Change this to increase distance between pairs of points
x_vals = np.arange(0,len(lowCont),1)
shifted_x_vals = np.arange(0+shift,len(highCont)+shift,1)

# loop through the data and plot the pairs of points to join them by a line
for x,x1,y,y1 in zip(x_vals,shifted_x_vals,lowCont,highCont):
    plt.plot([x,x1], [y,y1], color="k")

plt.scatter(x_vals, lowCont, color = 'r', label = 'label1')
plt.scatter(shifted_x_vals, highCont, color = 'k', label = 'label2')

# set ticks to between the two points
plt.xticks(x_vals + (shift/2), IDs, size='small')

plt.errorbar(x_vals, lowCont,yerr=lowContErr, linestyle="None", color = 'r', capsize=3)
plt.errorbar(shifted_x_vals, highCont,yerr = highContErr,linestyle = "None", color = 'k', capsize=3)

plt.xlabel('x')
plt.ylabel('y')
plt.title('graph title')
plt.legend()

plt.show()

How to draw a scatter plot with multiple y values for each x value?


By : Francis S Manaig
Date : March 29 2020, 07:55 AM
Hope that helps The traditional D3 answer here would be appending a group for each object and then appending a circle for each y value for each group.
However, since you seem to be a D3 beginner (correct me if I'm wrong), I'd suggest to just create a single array of objects, that you can pass to data.
code :
const newData = data.reduce(function(a, c) {
  return a.concat(c.y.map(function(d) {
    return {
      x: c.x,
      y: d
    }
  }));
}, []);
const data = [{
    x: 0.2,
    y: [1, 2, 4]
  },
  {
    x: 0.3,
    y: [2]
  },
  {
    x: 0.5,
    y: [4, 7, 8, 12, 19]
  }, {
    x: 1.4,
    y: [1, 3]
  }
];

const newData = data.reduce(function(a, c) {
  return a.concat(c.y.map(function(d) {
    return {
      x: c.x,
      y: d
    }
  }));
}, []);

const x = d3.scaleLinear()
  .domain([0, 2])
  .range([0, 300]);

const y = d3.scaleLinear()
  .domain([0, 20])
  .range([0, 150]);

const svg = d3.select("svg");
svg.selectAll("circle")
  .data(newData)
  .enter()
  .append("circle")
  .attr("cx", function(d) {
    return x(d.x)
  })
  .attr("cy", function(d) {
    return y(d.y)
  })
  .attr("r", 4)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg></svg>

How to create a scatter plot where values are across multiple columns?


By : Agustin Viñao
Date : March 29 2020, 07:55 AM
Hope this helps Rather than indexing, I think you need to better understand how pandas and matplotlib interact each other.
Let's go by steps for your case:
code :
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

#Here I copied you data in a data.txt text file and import it in pandas as a csv.
#You may have a different way to get your data.
df = pd.read_csv('data.txt', sep='\s+', engine='python')

#I assume to have a column named 'time' which is set as the index, as you show in your post.
df.set_index('time')

tdf = df.transpose() #transpose the dataframe

#Drop the time column from the trasponsed dataframe. time is not a data to be plotted.
tdf = tdf.drop('time')

#Creating x values, I go for 1 to 5 but they can be different.
tdf['xval'] = np.arange(1, len(tdf)+1)

#Choose a colormap and making a list of colors to be used.
colormap = plt.cm.rainbow
colors = [colormap(i) for i in np.linspace(0, 1, len(tdf))]

#Make an empty plot, the columns will be added to the axes in the loop.
fig, axes = plt.subplots(1, 1)
for i, cl in enumerate([datacol for datacol in tdf.columns if datacol != 'xval']):
    tdf.plot(x='xval', y=cl, kind="scatter", ax=axes, color=colors[i])

plt.show()

How to make a scatter plot with varying scatter size and color corresponding to a range of values from a dataframe?


By : user3367289
Date : March 29 2020, 07:55 AM
hope this fix your issue You could use pandas.cut to create a couple of helper columns in df based on your color and size mappings. This should make it easier to pass these arguments to pyplot.scatter.
N.B. It's worth noting that the values you've chosen for size may not distinguish the markers very well in the plot - it'd be worth experimenting with different sizes until you get the desired results
code :
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df['color'] = pd.cut(df['Depth'], bins=[-np.inf, 30, 40, 60, np.inf], labels=['red', 'blue', 'black', 'yellow'])
df['size'] = pd.cut(df['Magnitude'], bins=[-np.inf, 2, 3, 4, np.inf], labels=[1, 1.5, 2, 2.5])

plt.scatter(df['Lon'], df['Lat'], c=df['color'], s=df['size'])
def magnitude_size(magnitude):
    if magnitude < 2 :
        return 1
    if magnitude >= 2 and magnitude < 3 :
        return 1.5
    if magnitude >= 3 and magnitude < 4 :
        return 2
    if magnitude >= 4  :
        return 2.5


def depth_color(depth):
    if depth < 30 :
        return 'red'
    if depth >= 30 and depth < 40 :
        return 'blue'
    if depth >= 40 and depth < 60 :
        return 'black'
    if depth >= 60  :
        return 'yellow'
    if np.isnan(depth):
        return 'green'

di = {
    'size': df.Magnitude.apply(magnitude_size),
    'color' : df.Depth.apply(depth_color)
}

plt.scatter(df.Lon,df.Lat,c=di['color'],s=di['size'])
Related Posts Related Posts :
  • Add two mutated column in a grouping by with data.table
  • Why is the type integer?
  • How to write a nested IF and RAND excel statement
  • apply chisq.test for contingency table over a dataframe using 4 row observations
  • efficient way to create a new variable from multiple columns in R dataframe
  • set names with magrittr where both name and value are variable of data.frame?
  • Rule tables to avoiding nested ifelse statements
  • How to compare duplicated values and filter out unwanted ones in a R data frame?
  • How to add legends in this context?
  • How to sort single letters before double letters
  • R Stacked percentage bar plot with percentage of two factor variables with ggplot
  • Calculate date of birth given age
  • I want to convert scatter plot to a heat map tile plot over a tennis court image
  • Series of correlation matrices in R
  • binomial regression model produces glm.fit error
  • Create multiple lists with one command
  • Calculate n-dimensional euclidean distance from group centroids for each sample and select the lowest 3 for each group i
  • RSQLite: How to increase number of columns and parameters?
  • How to get line number of a function call in R?
  • about ggsave in superheat package
  • Multiple Select All buttons causing problems with interactivity in Shiny map
  • Why does it say my tree has no branch lengths when it does?
  • How to match row to column when 'Yes' is entered in multiple columns
  • read.fwf does not read all lines in a fixed width files
  • Is there any R code to repeat a same value for multiple rows?
  • Problems replacing character values in a dataframe in R
  • More efficient way to perform calculations on multiple (combined) columns by group
  • Add titles to facets
  • using 'by()' descriptive statistics are calculated in groups
  • What does %T>% function mean in R?
  • Improve speed of r 'for loop'
  • Citations in DT:datatable
  • Addressing to subsequent objects in loop function in R
  • How to delete from vector using ifelse condition in R
  • Perform large no. of iterations efficiently without using for loops
  • Need to create ggplot heatmap with NA-only data, but get "Error: Must request at least one colour from a hue palett
  • Unable to use "count" function in R
  • Applying a function to the last result in R with minimal typing
  • R : Find Column with highest value
  • A function in R for "joining"/"concantenating" word lists
  • Change/Map levels of a data frame columns using other reference list in R
  • My package is not recognizing the exported functions of an imported package in R
  • dplyr / tidy way to filter a vector based on a substring?
  • Reverting to previous ordering of factor after using relevel() and fitting GLM in R
  • integrating R dataframes and performing operations based on column values
  • r add background color to geom_label()
  • R call different variables dynamically
  • How to access data name from a list of time-series in R?
  • Time Series plot.ts and x label
  • Specify order of sequence with paste and rep functions
  • How can I loop over each row in R and see if a condition is met to change the format of a date?
  • Loop in R to find the max of a matrix
  • Harvard citation format in R markdown
  • In R when using accessor "[", what is the default behavior when using on data frames?
  • histogram in R with for loop
  • Keep specific date range before and after a timestamp
  • Extract dates in various formats from string in R
  • Get (web) url for help documentation in R?
  • Mean of 3 dataframes with same dimensions
  • Calculate adstock using data.table
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk