I'm trying to make a shiny dashboard with attributes of superheros graphed in a radar plot. I'd also like to be able to use radio buttons to select which of the superheros I'd like to see in the graph. However, when I run this code, I get an error that says: Error in polygon: invalid value specified for graphical parameter "lwd". There is no lwd command that I'm aware of for radar charts so I'm not sure how to correct this. Does anyone have a suggestion on how to handle this error?
library(fmsb)
data<-data.frame(Strength = c(7, 0, 6, 7, 4, 3),
Speed = c(7, 0 , 5, 7, 3, 2),
Intelligence = c(7, 0, 6, 2, 4, 3),
Fighting_Skills = c(7, 0, 4, 4, 4, 6),
Energy = c(7, 0, 6, 6, 1, 1),
Durability = c(7, 0, 6, 6, 3, 3),
row.names = c("max", "min", "Iron Man", "Thor", "Spiderman", "Captain America"))
head(data)
colors_fill<-c(scales::alpha("gray", 0.1))
#scales::alpha("gold", 0.1),
#scales::alpha("tomato", 0.2),
#scales::alpha("skyblue", 0.2))
colors_line<-c(scales::alpha("darkgray", 0.9))
#scales::alpha("gold", 0.9),
#scales::alpha("tomato", 0.9),
#scales::alpha("royalblue", 0.9))
#radarchart(data,
#seg =7,
#title = "Radar Chart",
#pcol = colors_line,
#pfcol = colors_fill,
#plwd = 1)
#legend(x = 0.6,
#y=1.35,
#legend = rownames(data[-c(1,2),]),
# bty = "n", pch = 20, col = colors_line, cex = 1.2, pt.cex = 3)
# Define UI for application
ui <- fluidPage(
# Application title
titlePanel("Radar chart"),
# Sidebar with a radio buttons for person
sidebarLayout(
sidebarPanel(
radioButtons("variablechoice", "People Choice",
choices = c("Iron Man", "Thor", "Spiderman", "Captain America"),
selected = "Thor")
),
# Show a plot
mainPanel(
plotOutput("radar")
)
)
)
# Define server logic required to draw a radar plot
server <- function(input, output) {
output$radar <- renderPlot({
if( input$variablechoice=="Iron Man"){new<-data[c(3),] }
if( input$variablechoice=="Thor"){new<-data[c(4),] }
if( input$variablechoice=="Spiderman"){new<-data[c(5),] }
if( input$variablechoice=="Captain America"){new<-data[c(6),] }
radarchart(new,
seg = 7,
#title = "Radar Chart",
pcol = colors_line,
pfcol = colors_fill,
plwd = 0.5)
})
}
# Run the application
shinyApp(ui = ui, server = server)
The issue is that you have missed to include the first two rows of your data which contain the min and max values for the categories in your new dataframe. That's why radarchart throws an error:
library(fmsb)
library(shiny)
data <- data.frame(
Strength = c(7, 0, 6, 7, 4, 3),
Speed = c(7, 0, 5, 7, 3, 2),
Intelligence = c(7, 0, 6, 2, 4, 3),
Fighting_Skills = c(7, 0, 4, 4, 4, 6),
Energy = c(7, 0, 6, 6, 1, 1),
Durability = c(7, 0, 6, 6, 3, 3),
row.names = c("max", "min", "Iron Man", "Thor", "Spiderman", "Captain America")
)
colors_fill <- c(scales::alpha("gray", 0.1))
colors_line <- c(scales::alpha("darkgray", 0.9))
# Define UI for application
ui <- fluidPage(
# Application title
titlePanel("Radar chart"),
# Sidebar with a radio buttons for person
sidebarLayout(
sidebarPanel(
radioButtons("variablechoice", "People Choice",
choices = c("Iron Man", "Thor", "Spiderman", "Captain America"),
selected = "Thor"
)
),
# Show a plot
mainPanel(
plotOutput("radar")
)
)
)
# Define server logic required to draw a radar plot
server <- function(input, output) {
output$radar <- renderPlot({
if (input$variablechoice == "Iron Man") {
new <- data[c(1:2, 3), ]
}
if (input$variablechoice == "Thor") {
new <- data[c(1:2, 4), ]
}
if (input$variablechoice == "Spiderman") {
new <- data[c(1:2, 5), ]
}
if (input$variablechoice == "Captain America") {
new <- data[c(1:2, 6), ]
}
radarchart(new,
seg = 7,
# title = "Radar Chart",
pcol = colors_line,
pfcol = colors_fill,
plwd = 0.5
)
})
}
# Run the application
shinyApp(ui = ui, server = server)
I have several stacked column series displayed as "totalPercent".
For example:
var series = chart.series.push(new am4charts.ColumnSeries());
series.dataFields.valueY = "footfall";
series.dataFields.valueYShow = "totalPercent";
series.dataFields.dateX = "datetime";
These display properly.
I'm trying to add a line to my series' of stacked columns.
For example:
var trend = chart.series.push(new am4charts.LineSeries());
trend.dataFields.valueY = "benchline";
trend.dataFields.dateX = "datetime";
trend.data = [ {"datetime": new Date(2021, 3, 23, 10),"benchline": 65},
{"datetime": new Date(2021, 3, 24, 13),"benchline": 65},
{"datetime": new Date(2021, 3, 26, 13),"benchline": 65}];
trend.strokeWidth = 2
trend.stroke = trend.fill = am4core.color("#c00");
trend.legendSettings.labelText = "Demos benchmark";
The 'benchline' values get included in the total from which the 'totalPercent' is calculated for the stacked columns, squashing the first 3 (same number as I have points in my LineSeries).
Is there some way to include the LineSeries without distorting the columns?
Fiddle with full code.
The trick was to add a second axis and attach the LineSeries to that axis.
var valueAxis2 = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis2.title.text = "Percent";
valueAxis2.renderer.opposite = true;
valueAxis2.min = 0;
valueAxis2.max = 100;
valueAxis2.strictMinMax = true;
valueAxis2.renderer.labels.template.adapter.add("text", function(text) {
return text + "%";
});
var trend = chart.series.push(new am4charts.LineSeries());
trend.dataFields.valueY = "benchline";
trend.dataFields.dateX = "datetime";
trend.data = [ {"datetime": new Date(2021, 3, 23, 10),"benchline": 65},
{"datetime": new Date(2021, 3, 24, 13),"benchline": 65},
{"datetime": new Date(2021, 3, 26, 13),"benchline": 65}];
trend.strokeWidth = 2
trend.stroke = trend.fill = am4core.color("#c00");
trend.legendSettings.labelText = "Demos benchmark";
trend.yAxis = valueAxis2;
I have created Highcharts graph by this code:
def chart_data(request):
dataset = DispatchPlan.objects.annotate(month=TruncMonth('scheduled_date')).values('month').annotate(
c=Sum('weight')).values('month', 'c')
chart = {
'chart': {'type': 'column'},
'title': {'text': 'Weight Dispatched by Months'},
'series': [{
'name': 'Months',
'data': [{'name': row['month'], 'y': row["c"]} for row in dataset]
}]
}
return JsonResponse(chart)
How can I add the X axis labels such that it shows month name instead of 0 and 1 ?
This is the one row of dataset from which the graph is plotted
{'month': datetime.datetime(2019, 6, 1, 0, 0, tzinfo=<DstTzInfo 'Asia/Kolkata' IST+5:30:00 STD>), 'c': 17600}
Try to use strftime (documentation) like that :
{'month': datetime.datetime(2019, 6, 1, 0, 0, tzinfo=<DstTzInfo 'Asia/Kolkata' IST+5:30:00 STD>).strftime("%B"), 'c': 17600}
names=['Peter', 'John']
size = ['X', 'M', 'L']
list_price = [1, 2, 3, 4, 5, 6] # There are 2 people will buy 3 size of shirt
I want to create my data structure into:
[
{'name': u'Peter', 'size_price': defaultdict(<type 'int'>, { 'X': 1, 'M':2, 'L': 3})},
{'name': 'John', 'size_price': defaultdict(<type 'int'>, {'X':4, 'M':5, 'L':6})}
]
I prefer to do defaultdict()
You can turn list_price into an iterator and then use next to get one value after the other:
>>> iterator = iter(list_price)
>>> [{"name": n, "size_price": {s: next(iterator) for s in size}} for n in names]
[{'size_price': {'X': 1, 'M': 2, 'L': 3}, 'name': 'Peter'},
{'size_price': {'X': 4, 'M': 5, 'L': 6}, 'name': 'John'}]
Of course you do not have to use a list comprehension but can do the same thing with nested loops as well.
Looks simple but I am not able to draw a X-Y chart with "dots" in pandas DataFrame.
I want to show the subid as "Mark" on X Y Chart with X as age and Y as fdg .
Code so far
mydata = [{'subid': 'B14-111', 'age': 75, 'fdg': 3}, {'subid': 'B14-112', 'age': 22, 'fdg': 2}, {'subid': 'B14-112', 'age': 40, 'fdg': 5}]
df = pandas.DataFrame(mydata)
DataFrame.plot(df,x="age",y="fdg")
show()
df.plot() will accept matplotlib kwargs. See the docs
mydata = [{'subid': 'B14-111', 'age': 75, 'fdg': 3}, {'subid': 'B14-112', 'age': 22,
'fdg': 2}, {'subid': 'B14-112', 'age': 40, 'fdg': 5}]
df = pandas.DataFrame(mydata)
df = df.sort(['age']) # dict doesn't preserve order
df.plot(x='age', y='fdg', marker='.')
Reading your question again, I'm thinking you might actually be asking for a scatterplot.
import matplotlib.pyplot as plt
plt.scatter(df['age'], df['fdg'])
Have a look at the matplotlib docs.
Try following for a scatter diagram.
import pandas
from matplotlib import pyplot as plt
mydata = [{'subid': 'B14-111', 'age': 75, 'fdg': 3}, {'subid': 'B14-112', 'age': 22,
'fdg': 2}, {'subid': 'B14-112', 'age': 40, 'fdg': 5}]
df = pandas.DataFrame(mydata)
x,y = [],[]
x.append (df.age)
y.append (df.fdg)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(y,x,'o-')
plt.show()